Newbie question, the >>, << an

# Thread: Newbie question, the >>, << an

1. Senior Member
Join Date
Dec 1969
Posts
635

## Newbie question, the >>, << an

Ok, so I&#039;ve been programming for 4 years now. After lots & lots of javascript programming, I still don&#039;t know what the &#062;&#062;, &#060;&#060; and &#062;&#062;&#062; do.<BR><BR>Actually, yes, but I don&#039;t know how I could use them in my day-to-day programming.<BR><BR>First, let&#039;s see if I&#039;m correct in the meaning of &#060;&#060; and &#062;&#062;<BR><BR>&#060;&#060; will perform a binary left shift of the variable.<BR><BR>Meaning that in 11 &#060;&#060; 1 would give : 6<BR>1011 &#060;&#060; 1 = 0110 .... right ?<BR><BR>11 &#062;&#062; 1 would give : 5<BR>1011 &#062;&#062; 1 = 0101<BR><BR>Am i correct so far ?<BR><BR>Then..what does &#062;&#062;&#062; do ?? I did my homework and went to read<BR><BR><BR>(Zero-fill 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

2. Senior Member
Join Date
Dec 1969
Posts
96,118

## Close, but no cigar...

First of all, all the numbers you shift in JS code will be *32* bits long. I&#039;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 &#060;&#060; 1 = 00010110 (or 22 decimal)<BR><BR>Makes sense. Shifting left by one doubles the number.<BR><BR>**QUOTE**<BR>11 &#062;&#062; 1 would give : 5<BR>**END**<BR>Huh?<BR><BR>00000011 &#062;&#062; 1 gives 00000001 (1 decimal)<BR><BR>But you were right saying<BR><BR>00001011 &#062;&#062; 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 &#062;&#062;&#062; 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 &#062;&#062; operator, then <BR>11111111 11111111 11111111 11111111 &#062;&#062; 1 is 11111111 11111111 11111111 11111111 <BR>STILL -1 !!<BR><BR>Or<BR>11111111 11111111 11111111 11111110 &#062;&#062; 1 is 11111111 11111111 11111111 11111111 <BR>(-2 decimasl) divided by 2 is (-1 decimal)<BR><BR>But if you use &#062;&#062;&#062; then<BR>11111111 11111111 11111111 11111111 &#062;&#062;&#062; 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>

3. Senior Member
Join Date
Dec 1969
Posts
635

## 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 &#062;&#062; 1 I meant 11 decimal right shift 1 binary (1011 &#062;&#062; 1 = 5 [0101])<BR><BR>Thanks a lot for your help again.<BR><BR>Eniac

4. Senior Member
Join Date
Dec 1969
Posts
96,118

## LOL! Teach you...

...top pick 11 decimal as an example!<BR><BR>If you&#039;d just used (say) 14 as the example then I wouldn&#039;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
•