Rating changes for last rounds are temporarily rolled back. They will be returned soon. ×

daihan's blog

By daihan, 4 years ago, In English

We know that builtin_popcount returns the number of 1 bits in a number . For example: Binary representation of 5 is 101 and __builtin_popcount(5) will return 2 .

But how it works for negative number ? for example:

  1. Binary representation of -8 equals = 1000 but __builtin_popcount(-8) returns 29 . Shouldn't it be return 1 ? Because there are one '1' bits in -8 .

  2. Binary representation of -7 equals = 1001 but __builtin_popcount(-7) returns 30 . Shouldn't it be return 2 ? Because there are two '1' bits in -7 .

  3. Binary representation of -6 equals = 1010 but __builtin_popcount(-6) returns 30 . Shouldn't it be return 2 ? Because there are two '1' bits in -6 .

  4. Binary representation of -5 equals = 1011 but __builtin_popcount(-5) returns 31 . Shouldn't it be return 3 ? Because there are three '1' bits in -5 .

Or __builtin_popcount(x) works only for positive numbers?

  • Vote: I like it
  • -25
  • Vote: I do not like it