-1
$a = 23;
$res = $a << 4 
print($res);

For the code snippet pasted above, the output is 368. How is it being calculated?

I expected 92.

a is 23

a in base 2 is 10111

so a in 8 bits is 00010111

Left shift 4 is 01110000

It is - 92

Can some body explain me ?

ajaysinghdav10d
  • 1,771
  • 3
  • 23
  • 33

2 Answers2

2

This is why it is returning 368 because $a << $b Shift the bits of $a $b steps to the left

23

256 128 64 32 16 8 4 2 1
 X   X   X  X  1 0 1 1 1

after $a << 4

368

256 128 64 32 16 8 4 2 1
 1   0   1  1  1 0 0 0 0 
A l w a y s S u n n y
  • 36,497
  • 8
  • 60
  • 103
1

Check bitwise operator manual here

Convert 23 in binary and it is 10111 and when you perform shift left bitwise operator, its value will be 101110000 and when you convert it to decimal it becomes 368.

akshaypjoshi
  • 1,245
  • 1
  • 15
  • 24