which is the rule for binary shifting of a negative number | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

which is the rule for binary shifting of a negative number

example -7 >> 3

15th Sep 2017, 6:59 AM
Oma Falk
Oma Falk - avatar
8 Answers
+ 2
it works quite simple: y<<x is equivalent y*2**x y>>x is equivalent y//2**x (so -7//2**3 == -7//8 == -1 (floor division))
15th Sep 2017, 9:29 AM
yuri
+ 4
In which language? All language is different about this
15th Sep 2017, 7:11 AM
Yanothai Chaitawat
Yanothai Chaitawat - avatar
+ 3
7= 0000 0000 0000 0000 0000 0000 0000 0111 -7= 1111 1111 1111 1111 1111 1111 1111 1001 negative just flip all bits and do addition by one -7>>3= 0001 1111 1111 1111 1111 1111 1111 1111 Sorry I might be wrong bc I'm busy [Edited] -7>>3= 1111 1111 1111 1111 1111 1111 1111 1111 shift in negative will fill 1 instead 0
15th Sep 2017, 7:28 AM
Yanothai Chaitawat
Yanothai Chaitawat - avatar
+ 3
Actually the >> right bit shift is pretty standard across multiple languages. The right bit shift shifts the binary value of x to the right y times (x >> y) dropping off the right most value of the binary number and left fills a copy of the left most number. I.E. A positive number is 0 (zero) filled on the left while a negative number is 1 (one) filled from the left. a 16 bit binary number with the value of -10 would look like: 1111 1111 1111 0110 so if we right shifted it 2 times it would look like: 1111 1111 1111 1101 which has the value of -3 if we right shifted it 2 more times: 1111 1111 1111 1111 we now have -1. At this point it won't matter how many times we right shift, the value will not go any higher than -1
15th Sep 2017, 7:33 AM
ChaoticDawg
ChaoticDawg - avatar
+ 2
how do i have to calculate it (language = pencil).
15th Sep 2017, 7:23 AM
Oma Falk
Oma Falk - avatar
+ 2
so i calculate -7 >> 3 = -1 great stuff . i undetstand.
15th Sep 2017, 7:48 AM
Oma Falk
Oma Falk - avatar
+ 2
so 47>>3 = 5 and -47>>3 = -6
15th Sep 2017, 9:53 AM
Oma Falk
Oma Falk - avatar
+ 2
yuri... it is indeed quiet simple!
15th Sep 2017, 3:31 PM
Oma Falk
Oma Falk - avatar