What happens with a bit shift for all 8 bits

I have a small query in c, I use a bitwise left shift at number 69, which is 01000101in binary

01000101  << 8 

and I get the answer as 100010100000000

There should not be only 8 zeros, i.e. 00000000when we shift all 8 bits to the left and then apply zeros.

+4
source share
2 answers

It is because of the literal (default data type) for the number ( int) in most modern processors that is larger than 8-bit(usually 32-bit), and therefore, when you apply

69 << 8 //note 69 is int

This actually applies as

00000000 00000000 00000000 01000101 << 8

So you get the result

00000000 00000000 01000101 00000000

, , unsigned char , :

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

, 69 << 8

01000101 00000000

8-bit unsigned char, :

00000000
+7

, . 69, , , 1 (, int, , 4 ), , ( / ) "" . :

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

1- , , .

           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.

, int 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
+4

All Articles