Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > .NET General

Vista - Question about bit shift

Reply
 
Old 07-15-2009   #1 (permalink)
Mars Shi


 
 

Question about bit shift

Hi,

I have some special requirements on bit shift by vb.net.

- I want to know how to get the shifted bit value.
- I want to know how to make shifted bit to another variable.

Suppose, I have two variables, byteA is &H0, byteB is &H8F

For the 1st question, if I left shift byteB, like byteB << 1, byteB will
be 00011110 (&H1E), but I want to get the bit being shifted, which is 1,
please tell me how I could get it.

For the 2nd question, I wish the shifted bit from byteB could be shifted
into byteA. The result I want is that, after operation, byteA would be
&H01, and byteB would be &H1E.

Also I wish variables could be byte, integer or long.

Is there any simple operator from VB.net?

Thanks

Mars


My System SpecsSystem Spec
Old 07-16-2009   #2 (permalink)
Morten Wennevik [C# MVP]


 
 

RE: Question about bit shift

Hi Mars

Regarding the first question, ALL bites get shifted one position left, but I
assume you mean which bits fall outside.

&H8F = 10001111
10001111 << 1 = 00011110

Well, since bit shifting is actually a 32 bit operation you end up with

00000000 00000000 00000001 00011110

compared to a byte you get the bits shifted by right shifting 8

00000000 00000000 00000001 00011110 >> 8
00000000 00000000 00000000 00000001

In C# you actually get a compiler error if you try to store the shifted
result in a byte.

byte b = 0x8F;
int c = b << 1; // = 0x11E

In VB the compiler will cast the result to the type of b, which means you
will lose the shifted bits. I believe you will need to define b as an
Integer to get the result as an Integer

Dim byteB As Integer = &H8F
Dim byteC = byteB << 1 ' = &H11E

Anyhow, if the bits shifted left falls outside you need to store them before
you shift. You can easily find them by shifting the original value 8 - #
places right in case you shift a byte, 16 - # for short and 32 - # for
integer, and 64 - # for long.

Dim byteShifted = byteB >> (8 - 1)

--
Happy Coding!
Morten Wennevik [C# MVP]


"Mars Shi" wrote:
Quote:

> Hi,
>
> I have some special requirements on bit shift by vb.net.
>
> - I want to know how to get the shifted bit value.
> - I want to know how to make shifted bit to another variable.
>
> Suppose, I have two variables, byteA is &H0, byteB is &H8F
>
> For the 1st question, if I left shift byteB, like byteB << 1, byteB will
> be 00011110 (&H1E), but I want to get the bit being shifted, which is 1,
> please tell me how I could get it.
>
> For the 2nd question, I wish the shifted bit from byteB could be shifted
> into byteA. The result I want is that, after operation, byteA would be
> &H01, and byteB would be &H1E.
>
> Also I wish variables could be byte, integer or long.
>
> Is there any simple operator from VB.net?
>
> Thanks
>
> Mars
>
>
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Need For Speed Shift Gaming
SHIFT KEY OUT OF CONTROL Vista General
shift of view Vista General
shift key problems Vista General
Eratic shift key Vista General


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46