I put this together to help some others...I could use some input on shifting of negative numbers but the remainder should be useful! I have it in a
Word 97 document and am not sure how the formating will show up as I am copying/pasting it in here. If anyone would like the word document emailed, leave me you email address and I'll forward it.
***********************************
Numeric Conversions and Bit Shifting
To convert a decimal number into binary, octal or hexadecimal, you need to understand �powers� of each base system and be able to figure the positional value representation. If I would have to convert from octal to binary, or hexadecimal to binary, I would convert the original value to decimal and then convert to the second base system. There may be an easier way but this works for me!
Also, if you need to shift a negative number, get the positive value in binary and then reverse the bits. Do your shifting, and then reverse your bits again to find the value of the shifted number. Remember that your number could be positive or negative based on the type of shift you do � get the value first, then make the sign appropriate for the shift.
Binary positional valuesBASE 2 (x2 or to the power of 2)
5096204810245122561286432168421
xxxxx
binary #10110011
positional value128032160021
|||||
128||||
32-------------|||
16---------------------||
2---------------------------------------------|
1-----------------------------------------------------
total decimal value179
When doing bit shifting, the left operand is assumed to be an int (32 bits) unless it is specifically stated to be a long (64 bits). The shift will be performed upon the left operand value the number of times stated by the right hand operand. Something to remember is that shifting is only able to be performed by the number of bits available � 32 or 64. If you are given a shift larger than the bit value of the left operand you need to modulo by the bits available. In our instance above 179 (int) >> 45 is actually 179 >> 13 (45 % 32).
The effect of >> is dividing by 2 for every shift (remainders are truncated, not rounded) with the filled in bits being zeros for positive numbers and ones for negative numbers. Using the example above, 179 >>> 2 is the same as ((179 / 2 = 89) / 2 = 44). See the example below.
5096204810245122561286432168421
xxx
binary # right shifted 2101100
positional value3208400
|||
32||
8-------------|
4---------------------
total decimal value44
The effect of >>> is the same except the filled in bits are always 0, automatically making negative numbers positive.
The effect of << is multiplying by 2 for every shift. The filled in bits are always 0. Depending on the size of the shift, a positive number could be negative and a negative number could be positive.
Octal positional valuesBASE 8 (x8 or to the power of 8)
20971522621443276840965126481
xxxxx
Octal # starts with 017426
positional value40963584256166
|||||
4096---------------------------||||
3584-----------------------------------|||
256-------------------------------------------||
16---------------------------------------------------|
6-----------------------------------------------------------
total decimal value7958
Hexidecimal positional valuesBASE 16 (x16 or to the power of 16)
0 to 9 then A to F
1048576655364096256161
xxxxxA=10
Hex # starts with 0x196AEB=11
positional value6553636864153616014C=12
|||||D=13
65536---------------------------||||E=14
36864-----------------------------------|||F=15
1536-------------------------------------------||
160---------------------------------------------------|
14-----------------------------------------------------------
total decimal value104110
Hopefully this helps!
Lori