What happens with bitwise shift for all 8 bits

11,628

Solution 1

It is because of the literal (default data type) for a number (int) is, in most of nowadays CPU, greater than 8-bit (typically 32-bit) and thus when you apply

69 << 8 //note 69 is int

It is actually applied like this

00000000 00000000 00000000 01000101 << 8

Thus you get the result

00000000 00000000 01000101 00000000

If you use, say, unsigned char specifically, then it won't happen:

unsigned char a = 69 << 8; //resulting in 0

This is because though 69 << 8 itself will still result in

01000101 00000000

But the above value will be casted to 8-bit unsigned char, resulting in:

00000000

Solution 2

Bit shift operators act on entire objects, not individual bytes. If the object storing 69 is wider than 1 byte (int is typically 4 bytes for example), then the bits that are shifted outside of the first (lowest/rightmost) byte overflow and are "pushed into" the second byte. For example:

00000000 00000000 00000000 01000101 //The number 69, stored in a 32 bit object
00000000 00000000 01010000 00000000 //shifted left by 8

If you had stored the number in a 1-byte variable, such as a char, the result would indeed have been zero.

           01000101 //The number 69, stored in an 8 bit object
(01000101) 00000000 //shifted left by 8
 ^^^^^^^^
these bits have been shifted outside the size of the object.

The same thing would happen if you shifted an int by 32.

00000000 00000000 00000000 01000101 //The number 69, stored in a 32 bit int
00000000 00000000 01010000 00000000 //shifted left by 8
00000000 01010000 00000000 00000000 //shifted left by 16
01010000 00000000 00000000 00000000 //shifted left by 24
00000000 00000000 00000000 00000000 //shifted left by 32, overflow
Share:
11,628

Related videos on Youtube

TechJ
Author by

TechJ

Updated on June 04, 2022

Comments

  • TechJ
    TechJ almost 2 years

    I have a small query in c, I am using the bitwise left shift on number 69 which is 01000101 in binary

    01000101  << 8 
    

    and I get answer as 100010100000000

    Shouldn't it be all 8 zeros i.e. 00000000 as we shift all the 8 bits to left and then pad with zeros.

    • SandBag_1996
      SandBag_1996 about 8 years
      depends how wide is your variable.. Looks like 16 bits
    • bruceg
      bruceg about 8 years
      if you say int i = 01000101 << 8; then i is 32 bits and the answer you see is expected.
    • TechJ
      TechJ about 8 years
      In my code i have used int
    • M.M
      M.M about 8 years
      All integer arithmetic (including shifts) is done in at least int precision
  • TechJ
    TechJ about 8 years
    So you means the answer is 00000000 00000000 01000101 00000000
  • wildplasser
    wildplasser about 8 years
    wrong. (bit shift) operators act on expressions, not variables.
  • Ian
    Ian about 8 years
    @TechJ ah, yes. It depends on your CPU word length actually. But typically it is 32-bit.
  • TechJ
    TechJ about 8 years
    Thanks Ian for your answer.
  • ApproachingDarknessFish
    ApproachingDarknessFish about 8 years
    @wildplasser replaced "variable" with "object."
  • wildplasser
    wildplasser about 8 years
    It is not an object (it need not have an addres, for instance) . It is an expression. consider the case (a+b) <<c Or a literal (like in the example): 1234 << x
  • ApproachingDarknessFish
    ApproachingDarknessFish about 8 years
    @wildplasser "A 32-bit expression" makes no sense to me. How do you suggest I rephrase my answer?