Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does bitwise & 0x3FF do?

Can someone explain the following line of code? Particularly, I don't get what (short) x & 0x3FF does?

int num = ... //some number.
return (short) num & 0x3FF;
like image 559
user1467855 Avatar asked Dec 04 '22 03:12

user1467855


1 Answers

It zeros out the top bits of the number, such that the result is always between 0 and 1023. It's essentially the same thing as modulo(num, 1024) (for positive values of num).

Without seeing a broader context it's impossible to know why this is here, but that's what it does.

like image 89
Tim Avatar answered Dec 23 '22 05:12

Tim