|
|
|
|
![]() ![]() |
Sep 28 2005, 12:26 PM
Post
#1
|
|
|
Neurotical Squirrel ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: [HOSTED] Posts: 579 Joined: 4-November 04 From: Novi Sad, Vojvodina Member No.: 2,127 |
This is for all Visual Basic programmers out there, who want to programm some form of encryption algorithm, or anything else that includes shifting bits left or right, for that matter.
Most of you who have tried to accomplish this, know that Visual Basic doesn't have any function for bit shifting. And bit shifting si very usefull Shifting bits is actualy easy, we use multiplication and division by powers of 2. For right shifting, we use division: CODE NewValue = Value \ (2 ^ BitsToShift) And for left shifting, we use multiplication: CODE NewValue = Value * (2 ^ BitsToShift) Now, that's not all... With Visual Basic, things can get a little trickier Trouble with VB is that it's Integer (16bit), and Long(32bit) data types are signed, and what that means they can hold both positive and negative values. One bit is used to hold the sign. All remaining bits are used to store actual value. Integer data type can store 65.535 numbers, but, since it is signed, it can only store numbers from -32.768 upto 32.767. Take a lok at this (for all you visual types, like me CODE DEC BIN HEX OCT 1 0000000000000001 &H1 1 2 0000000000000010 &H2 2 32.766 0111111111111110 &H7FFE 77.776 32.767 0111111111111111 &H7FFF 77.777 -32.767 1000000000000000 &H8000 100.000 -32.766 1000000000000001 &H8001 100.001 -2 1111111111111110 &HFFFE 177.776 -1 1111111111111111 &HFFFF 177.777 If you take the binary value of -1 (1111111111111111), and convert it to decimal in calculator, you wil get a value of 65.535. Same goes for hexadecimal and octal values... Neat, right Now, what does this have to do with bit shifting? It has everything to do, because during bit shifting to the right, sign bit is preserved in Visual Basic, and that gives wrong values in the end... Now, this explained, we can go on, and write that shifting function... SHIFTING BITS RIGHT In most cases, when people want to shift bits, they want to use unsigned shifting, that is, no negative values. When such a shift occurs, shift bit is not preserved. Now, this requires one extra step in order to work with Visual Basic, and it's signed data types. When value is negative, you need to AND it. Here's the example code CODE If Value < 0 Then NewValue = ((Value And &HFFFE) \ 2) And &H7FFF ' * Shift 1 bit right ' NewValue = ((Value And &HFFFC) \ 4) And &H3FFF ' * Shift 2 bits right ' NewValue = ((Value And &HFFF8) \ 8) And &H1FFF ' * Shift 3 bits right ' NewValue = ((Value And &HFFF0) \ 16) And &HEFFF ' * Shift 4 bits right ' NewValue = ((Value And &HFFE0) \ 32) And &HF7FF ' * Shift 5 bits right ' NewValue = ((Value And &HFFC0) \ 64) And &HFBFF ' * Shift 6 bits right ' NewValue = ((Value And &HFF80) \ 128) And &HFDFF ' * Shift 7 bits right ' NewValue = ((Value And &HFF00) \ 256) And &HFEFF ' * Shift 8 bits right Else NewValue = Value \ (2 ^ BitsToShift) End If SHIFTING BITS LEFT Shifting bits to the left, doesn't require any additional operations, it is simply multiplied by the power of 2, and is same for the signed and unsigned numbers: CODE NewValue = Value * (2 ^ BitsToShift) Some people recommend using Long data type, for Integer Values, in order to use unsigned bit shifting. Others, turning off 'Integer Overflow Check' in 'Advanced Optimisations'... Unsigned right bit shifting can't be tested from the Visual Basic IDE, instead, compile it into a project, turn off already mentined optimisation, and try it that way... That's all folks |
|
|
|
![]() ![]() |
Similar Topics
| Topics | Topics | |
|---|---|---|
|
|
|
|
Lo-Fi Version | Time is now: 20th July 2008 - 08:20 PM |