RE: RE: There can be only one!
by vroom (His Eminence) on May 15, 2000 at 21:27 UTC
|
They are bit-shift operators: They return the value of the left argument shifted left or right by the number of bits in the right argument.
2<<4 #returns 32;
32>>3 #returns 4;
vroom | Tim Vroom | vroom@cs.hope.edu
| [reply] [Watch: Dir/Any] [d/l] |
|
could you explain this a little more in depth? i don't quite get what you mean by shifting the bytes
| [reply] [Watch: Dir/Any] |
|
Numbers represented in binary format ie:
000000101
#5 in decimal 1*2^2+1*2^0
#this shifted 1 place to the left is equivalent to
#mutliplying by two
000001010
#this is equal to 10 or 1*2^3+1*2^1
| [reply] [Watch: Dir/Any] [d/l] |
|
It shifts bits, not bytes.
<< moves the bits left and >> moves the bits right.
2<<4 #2 is 00000010 in binary, the <<4 moves the bits left 4,00100000
+ which is 32
32>>3 #32 is 00100000 in binary, the >>3 moves the bits right 3, 00000
+100 which is 4
I hope this clears things up.
| [reply] [Watch: Dir/Any] [d/l] |
|
>> and << shift bits, not bytes. Numbers are internally
represented in digital computers using two-state bits.
The bitshift operators shift these bits.
For example 7 is 111 in binary. Shifting it one place left
results in 1110 (digits coming from off the edge are zero),
or 14. 111b shifted right once is 11 (a bit is shifted
off), or 3. 7*2=14, and 7/2 is 3.5, the .5 is lost because
of the one being shifted off.
Hope this helps.
| [reply] [Watch: Dir/Any] |
|
okay,
1 = 00000001
2 = 00000010
3 = 00000011
4 = 00000100
and so on......
if you did
2<<3, you would shift 2 3 bits......
so you would have 10000, or 16.
| [reply] [Watch: Dir/Any] |
|
A byte is 8 bits. A bit is a 1 or a 0 that spells out the
binary number corresponding to what you're trying to express.
2 in binary is 00000010. Shifting that to the left by
3 is this:
2 << 3 which yeilds 00010000 which is 8.
shifting bits is like taking an eight bit number and rotating
it in a certain direction.
| [reply] [Watch: Dir/Any] |
|
2 << 4 is 2 "left shifted" by four digits. In binary, 10.00000 is moved four digits to the left to result in 100000.0, or 32. Every time you move the decimal point in binary, you multiply or divide by two. So it's like moving the decimal point to multiply or divide by ten in decimal.
| [reply] [Watch: Dir/Any] |
|
this require some knowlege of binary numbers
00000010 = 2
if you shift that to the left four times
00100000 =32
| [reply] [Watch: Dir/Any] |