Caring for a piece of code

I watched one of the open source projects on github, and I found the following line of code in Java,

static byte[] byteArray = new byte[1 << 11]; 

here we know that 1 <11 is nothing but 2048, so I can directly initialize the array by specifying its length = 2048 as follows:

static byte[] byteArray = new byte[2048];

then why is it written as 1 <11 instead of 2048.

+7
java
source share
1 answer

The reason for using a slightly wise operation is to make it clear that it is a power of 2. I have seen people confuse a constant. for example 8096, which is a combination of 8192 and 4096.

I prefer to use << 10 or << 20 for KB and MB. e.g. 2 << 10 for 2 KB

+5
source share

All Articles