
Newbie question, the >>, << an
Ok, so I've been programming for 4 years now. After lots & lots of javascript programming, I still don't know what the >>, << and >>> do.<BR><BR>Actually, yes, but I don't know how I could use them in my daytoday programming.<BR><BR>First, let's see if I'm correct in the meaning of << and >><BR><BR><< will perform a binary left shift of the variable.<BR><BR>Meaning that in 11 << 1 would give : 6<BR>1011 << 1 = 0110 .... right ?<BR><BR>11 >> 1 would give : 5<BR>1011 >> 1 = 0101<BR><BR>Am i correct so far ?<BR><BR>Then..what does >>> do ?? I did my homework and went to read<BR><BR><BR>(Zerofill right shift) Shifts the first operand in binary representation the number of bits to the right specified in the second operand, discarding bits shifted off, and shifting in zeros from the left. <BR><BR><BR>the last part intrigues me....shifting in zeros from the left....<BR><BR>what does that mean ??<BR><BR><BR>But back to the original question, how could these operators help me in my usual programming??<BR><BR>Thanks.<BR><BR>Eniac

Close, but no cigar...
First of all, all the numbers you shift in JS code will be *32* bits long. I'll just show 8 of them here, but you need to imagine that all 32 are really there. <BR><BR>00001011 (11 decimal)<BR>00001011 << 1 = 00010110 (or 22 decimal)<BR><BR>Makes sense. Shifting left by one doubles the number.<BR><BR>**QUOTE**<BR>11 >> 1 would give : 5<BR>**END**<BR>Huh?<BR><BR>00000011 >> 1 gives 00000001 (1 decimal)<BR><BR>But you were right saying<BR><BR>00001011 >> 1 gives 00000101 (5 decimal)<BR><BR>Again, 00001011 is 11 decimal. Shifting right by 1 is the same as halving the number (and tossing away any remainder).<BR><BR>So 11 decimal divided by 2 is 5 remainder 1, or just 5 as integers go.<BR><BR>The only time >>> comes into play is when the number is negative. And now we have to show all 32 bits:<BR><BR>11111111 11111111 11111111 11111111 is 1 decimal<BR><BR>If you use the >> operator, then <BR>11111111 11111111 11111111 11111111 >> 1 is 11111111 11111111 11111111 11111111 <BR>STILL 1 !!<BR><BR>Or<BR>11111111 11111111 11111111 11111110 >> 1 is 11111111 11111111 11111111 11111111 <BR>(2 decimasl) divided by 2 is (1 decimal)<BR><BR>But if you use >>> then<BR>11111111 11111111 11111111 11111111 >>> 1 is 01111111 11111111 11111111 11111111<BR>which is 2147483647 decimal.<BR><BR>And at that I need to leave for the night.<BR><BR>

RE: Close, but no cigar...
Makes sense...dunno why I did not see it when I tried it (did some test in javascript before posting)<BR><BR>I probably would have understood if I had sticked to smaller numbers.<BR><BR>btw. by 11 >> 1 I meant 11 decimal right shift 1 binary (1011 >> 1 = 5 [0101])<BR><BR>Thanks a lot for your help again.<BR><BR>Eniac

LOL! Teach you...
...top pick 11 decimal as an example!<BR><BR>If you'd just used (say) 14 as the example then I wouldn't have gotten your decimal and binary confused.<BR><BR>Sorry for doubting you.<BR><BR>
Posting Permissions
 You may not post new threads
 You may not post replies
 You may not post attachments
 You may not edit your posts

Forum Rules

