Thread: Right Shift Operator

  1. #1
    Registered User
    Join Date
    Mar 2019
    Posts
    51

    Right Shift Operator

    64>>1 =32

    I understand it.

    But why,

    27>>1 = 13

    and not 13.5?

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    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
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Feb 2019
    Posts
    850
    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. #4
    Informer -Adrian's Avatar
    Join Date
    Jan 2013
    Posts
    830
    Code:
    32 16 8  4  2  1
    0  1  1  0  1  1    27
    0  0  1  1  0  1    27 >> 1

  5. #5
    Registered User
    Join Date
    Apr 2019
    Posts
    62
    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. #6
    Registered User
    Join Date
    Mar 2019
    Posts
    51
    Quote Originally Posted by flp1969 View Post
    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. #7
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    Quote Originally Posted by rm82co View Post
    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.
    Devoted my life to programming...

  8. #8
    Registered User
    Join Date
    Feb 2019
    Posts
    850
    Quote Originally Posted by rm82co View Post
    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?

    Right Shift Operator-gif-latex-gif
    Last edited by flp1969; 04-14-2019 at 07:34 PM.

  9. #9
    Registered User
    Join Date
    Mar 2019
    Posts
    51
    Quote Originally Posted by flp1969 View Post
    Study "two's complement representation", as GReaper suggested, and ask yourself: What happens when you subtract 1 from 0?

    Right Shift Operator-gif-latex-gif
    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. #10
    Registered User
    Join Date
    Feb 2019
    Posts
    850
    Quote Originally Posted by rm82co View Post
    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. #11
    Registered User
    Join Date
    Mar 2019
    Posts
    51
    Quote Originally Posted by flp1969 View Post
    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. #12
    Registered User
    Join Date
    Mar 2019
    Posts
    51
    Quote Originally Posted by rm82co View Post
    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. #13
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,905
    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
    Last edited by Click_here; 04-15-2019 at 07:41 PM. Reason: grammar
    Fact - Beethoven wrote his first symphony in C

  14. #14
    Registered User
    Join Date
    Feb 2019
    Posts
    850
    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:

    Right Shift Operator-png-latex-png

    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...
    Last edited by flp1969; 04-16-2019 at 05:58 AM.

  15. #15
    Registered User
    Join Date
    Apr 2017
    Location
    Iran
    Posts
    103
    Actually you would better use bit-wise operators for unsigned values.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. shift operator
    By Satya in forum C Programming
    Replies: 3
    Last Post: 01-05-2016, 11:56 AM
  2. Shift operator doubt
    By thannara123 in forum C Programming
    Replies: 3
    Last Post: 06-19-2015, 08:14 AM
  3. Bitwise Operator Shift
    By YannB in forum C Programming
    Replies: 14
    Last Post: 03-04-2014, 09:49 PM
  4. unsigned right shift operator
    By abraham2119 in forum C Programming
    Replies: 2
    Last Post: 06-05-2009, 11:01 AM
  5. left shift operator!!!!
    By theblackhat in forum C Programming
    Replies: 2
    Last Post: 10-02-2004, 02:07 AM

Tags for this Thread