What does the bitwise AND operator & do?

做~自己de王妃 提交于 2019-12-01 20:43:29

If you are referring to

a&=a-1;

then it is a bitwise and operation of a and a-1 copied into a afterwards.

Edit: As copied from Tadeusz A. Kadłubowski in the comment:

a = a & (a-1);

The expression a&=a-1; clears the least significant bit (rightmost 1) of a. The code counts the number of bits in a (-1 in this case).

Starting from

a = -1 ; // 11111111 11111111 11111111 11111111 32bits signed integer

The code outputs 32 on an 32 bit integer configuration.

& is the bitwise and operator.

The operation

a&=a-1;

which is same as:

a = a & a-1;

clears the least significant bit of a.

So your program effectively is calculating the number of bits set in a.

And since count is declared as static it will automatically initialized to 0.

you have count uninitialized

should be

static int count=0;

operator & is called AND http://en.wikipedia.org/wiki/Bitwise_operation#AND

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!