Java direct change

I saw this in java code.

int n = 300 //passed through a function size = (n + 31) >> 5 //size = 10 

What is the meaning of 5 ? What is the value of 31 // should do something with int size (31 bits + 1 character)

thanks

+4
source share
1 answer

The value of 5 is that 32 = 2 ^ 5.

 size = (n + 31) >> 5 

sets size to ceiling(n/32) , which is the number of 32-bit integers needed to store the flags of bit n .

Appendix 31 to n is to ensure that the dividend is not less than the smallest multiple of 32 greater than or equal to n .

+6
source

Source: https://habr.com/ru/post/1438523/