# 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
{"ft_lang_label":"__label__en","ft_lang_prob":0.9179827,"math_prob":0.9450845,"size":3458,"snap":"2021-21-2021-25","text_gpt3_token_len":928,"char_repetition_ratio":0.111754484,"word_repetition_ratio":0.03305785,"special_character_ratio":0.29988432,"punctuation_ratio":0.14794521,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.98745686,"pos_list":[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68],"im_url_duplicate_count":[null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,4,null,null,null,null,null,null,null,4,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,2,null,null,null,null,null,null,null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-06-12T14:23:30Z\",\"WARC-Record-ID\":\"<urn:uuid:cf1258bf-5839-4679-94fa-f1e9e7eae02a>\",\"Content-Length\":\"99903\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:6de99de9-fb71-46ed-a983-82a26d72428c>\",\"WARC-Concurrent-To\":\"<urn:uuid:c4d50e00-5a97-4ce9-b13e-92612e004271>\",\"WARC-IP-Address\":\"198.46.93.160\",\"WARC-Target-URI\":\"https://cboard.cprogramming.com/c-programming/177393-right-shift-operator.html?s=195624ebbc0c0ba229d709fc8ebeb9de\",\"WARC-Payload-Digest\":\"sha1:MB2FCH2V2LF66UHIKEO6QHVYSQXXX4E2\",\"WARC-Block-Digest\":\"sha1:SAMGOIEB7NNPGGGP5TFYETNXHRUHDWT4\",\"WARC-Identified-Payload-Type\":\"application/xhtml+xml\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-25/CC-MAIN-2021-25_segments_1623487584018.1_warc_CC-MAIN-20210612132637-20210612162637-00285.warc.gz\"}"}