left shift Operator cpp

96 Views Asked by At

i found this definition of CPP array in and competitive programming editorial

long long s1[1<<10][1<<10],s2[1<<10][1<<10]

why it is written like this we can simply write it like

 s1[1024][1024]

is there any reason behind this ?

2

There are 2 best solutions below

0
Hajo Kirchhoff On

1<<10 is a shorthand for "Bit 10 is set, all other bits are zero". You find this construct very often around lowlevel code, where powers of 2 or bits are important.

1<<10 == 0x400 == 010000000000

2
eerorika On

Constant bit shifts such as 1u << N are generally written to emphasise that the number is the Nth power of 2.

"Magic numbers" should be avoided, and small numbers like 1 and 10 are "less magical" than large numbers. I would still recommend using a named constant to clarify why 10 was used specifically.