right ( >> ) and left ( <<) shift , bitwise operators

Home Forums C-programming.. right ( >> ) and left ( <<) shift , bitwise operators

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #2595 Reply
    Humble
    Keymaster

    Right shift and left shift operators are bitwise operators:

    These are the thing to remember when performing these:

    x << 5 or x >> 5

    Please note that the number of bits to shift ( here 5) should be a positive value:

    Left shift is same on both unsigned and signed numbers

    Left shift is equivalant to multiplication by “2” ^ “n” where “n” is the number of bits asked to shift.

    When doing left shift the number of shifted bits are filled with “0”

    2 << 3 0000 0010 << 3 0 0010 000 which is equivalent to 2 * ( 2 ^3) = "16" in decimal . Right shift is different on signed and unsigned numbers:

    If its signed the shifted bits are filled with “0”s.

    Its equivalent to dividing the number with ( 2 ^ n ) where n is the number of bits:

    8 >> 3
    0000 1000 >> 3
    0000000 1 which is eqvalant to 8 / ( 2^3) = 1

    How-ever when you do right shift on signed numbers the vacent bits are filled with the sign bit if the machine is performing arithmetic shift or with 0s if the machine is performing logical shift:

    If you are using 2’s complement processor, the right shift on signed numbers will be arithmetic shift which means the bits are filled with signed bits..

    #2598 Reply
    Humble
    Keymaster
    This reply has been marked as private.
Viewing 2 posts - 1 through 2 (of 2 total)
Reply To: right ( >> ) and left ( <<) shift , bitwise operators
Your information: