# Thread: Right Shift Operator

1. ## Right Shift Operator

64>>1 =32

I understand it.

But why,

27>>1 = 13

and not 13.5? 2. Because its job isn't to divide by two. That is just a side-effect of its real function, which is to shift the bits of a number to the right by one. Not to mention that an integer can never be 13.5 3. And, beware: Right shifts are 'undefined behavior' if done with signed integers. Most compilers translate right shifts with signed integers to "arithmetic shifts", meaning the sign bit (the highest bit) is copied. So -1 >> 1 will still result in -1. The value -1, using two's complement representation is 0xffffffff (for 32 bits integers).

But other compilers/processors can do a 'logical shift', resulting in +2³¹-1 (0x7fffffff).

Anyway, both results tells us that right shifts aren't a division by powers of 2. And left shifts aren't multiplications by powers of 2 neither. Take the unsigned 0xffffffff (unsigned 4294967295), when shifted to the left 1 bit will result in 0xfffffffe (unsigned 4294967294).

Always do right shifts with 'unsigned' integers to avoid this. And never think of shifts as multiplications/divisions (unless you know what you are doing!). 4. Code:
```32 16 8  4  2  1
0  1  1  0  1  1    27
0  0  1  1  0  1    27 >> 1``` 5. You can only use the shift operator on integers. It doesn't make much sense to "shift" a floating point value. So where did that .5 go? It was discarded. If there's a 1 in the lower-most bit, it's shifted out and lost. 6. Originally Posted by flp1969 And, beware: Right shifts are 'undefined behavior' if done with signed integers. Most compilers translate right shifts with signed integers to "arithmetic shifts", meaning the sign bit (the highest bit) is copied. So -1 >> 1 will still result in -1. The value -1, using two's complement representation is 0xffffffff (for 32 bits integers).

But other compilers/processors can do a 'logical shift', resulting in +2³¹-1 (0x7fffffff).

Anyway, both results tells us that right shifts aren't a division by powers of 2. And left shifts aren't multiplications by powers of 2 neither. Take the unsigned 0xffffffff (unsigned 4294967295), when shifted to the left 1 bit will result in 0xfffffffe (unsigned 4294967294).

Always do right shifts with 'unsigned' integers to avoid this. And never think of shifts as multiplications/divisions (unless you know what you are doing!).
Tell me please that if 1 in binary is 000.....0001(32 bits) then why -1 is being shown as 111....1111(32 bits)? 7. Originally Posted by rm82co Tell me please that if 1 in binary is 000.....0001(32 bits) then why -1 is being shown as 111....1111(32 bits)?
Read this wikipedia page on the subject. 8. Originally Posted by rm82co Tell me please that if 1 in binary is 000.....0001(32 bits) then why -1 is being shown as 111....1111(32 bits)?
Study "two's complement representation", as GReaper suggested, and ask yourself: What happens when you subtract 1 from 0?  9. Originally Posted by flp1969 Study "two's complement representation", as GReaper suggested, and ask yourself: What happens when you subtract 1 from 0? OK! It is said that in C, int by default is a signed data type and if we requires unsigned then unsigned int x;
If it is the case, kindly let me know that what is the need of signed int x;
is it not same to int x; 10. Originally Posted by rm82co OK! It is said that in C, int by default is a signed data type and if we requires unsigned then unsigned int x;
If it is the case, kindly let me know that what is the need of signed int x;
is it not same to int x;
Did you understand what two's complement is? 11. Originally Posted by flp1969 Did you understand what two's complement is?
Yup! it is a way to indicate negative, zero and positive numbers. In one's complement, we used MSB for sign. But to carry that sign along was troubling. So, Two's complement was adopted as substitute. What happens? We take a number for example 2 and in binary 010 if for a moment consider 3 bits, now one's complement of 010 is 101 and add 1 in one's complement 111, is we get as two's complement and those great brain programmers decided to use it to indicate -2. Am I right? 12. Originally Posted by rm82co Yup! it is a way to indicate negative, zero and positive numbers. In one's complement, we used MSB for sign. But to carry that sign along was troubling. So, Two's complement was adopted as substitute. What happens? We take a number for example 2 and in binary 010 if for a moment consider 3 bits, now one's complement of 010 is 101 and add 1 in one's complement 111, is we get as two's complement and those great brain programmers decided to use it to indicate -2. Am I right?
Two's complement is resembling with the concept of floating point value in computer in my view. Floating point value is representing as scientific notation. So, 8 bits to represent to power. Now we use biased binary or offset binary to represent or IEEE754 ANSI i.e. 8 bits 2^8 = 256 or 2^7-1 = 127 shows offset for negative numbers. Now 128-256 binary patterns are for 0 and positive numbers. Now 129, i.e. in binary 1000 0001, might be showing +1 power. If I am not wrong. 13. Yup! it is a way to indicate negative, zero and positive numbers. In one's complement, we used MSB for sign.
Correct me if I am wrong, but I'm feeling like you are not getting it... A one's complement is not just setting the MSB, it's inverting all of the bits

If we have a 4 bit number (just for this example) representing the number 3, we have...
Code:
`3  => 0011`
Its one's complement will be (wiki)
Code:
```~number
=1100```
Its two's complement will be (wiki)
Code:
```~number + 1
1101``` 14. Elementary decimal subtraction: How do you subtract 8 from 22? You cannot subtract 8 from 2, so you BORROW '1' from the next right digit:

Code:
``` 22   ->   (2-1) (12)
-08   ->  -  0     8
-----     ------------
1     4```
In binary it works the same way, where "borrow" is a state kept by the processor. With single bits 0 - 1 = 1 + borrow=1. So, in a 4 bits word, 0000 - 0001 = 1111 + borrow=1), where this last bottow comes from the hypothetical bit 5.

If you use only the highest bit as an indicator of sign, like -1 = 1001, the math will be wrong for 0000 - 0001. If you use one's complement, -1 = 1110, and still wrong. That's why two's complement is the right way to represent negative values.

The two's complement comes from the equation: But, can be summarized in a single rule: Negative integers are represented by inverting all bits and adding 1.

Note this a representation. The actual value, in strict binary, is always positive (there is no way to insert the character '-' in a binary number, inside your computer). That said, signed int and unsigned int holds the same binary value. What is different is how they are interpreted... 15. Actually you would better use bit-wise operators for unsigned values. Popular pages Recent additions #### Tags for this Thread

bitwise operators, c language, operator, right shift, shift 