Why did we write 2^n as 1<<n?

In printSubsets function, to iterate over a range of numbers, (in example 0 to 8 where 3 is strlen). how did he use 2^n as 1<<n?? here, n is the length of the string

Left Shift : Denoted as : <<

Eg: N<<i (N: first operand, i: second operand)

Takes two numbers, left shifts the bits of the first operand, the second operand decides the number of places to shift. Or in other words left shifting an integer “x” with an integer “y” denoted as ‘(x<<y)’ is equivalent to multiplying x with 2^y (2 raised to power y).

eg: lets take N=22; which is 00010110 in Binary Form.

Now, if “N is left-shifted by 2” i.e N=N<<2 then N will become N=N*(2^2). Thus, N=22*(2^2)=88 which can be wriiten as 01011000

1 Like

Thank You Sir !! Got it