I'm trying to find the position of two 1's in a 64 bit number. In this case the ones are at the 0th and 63rd position. The code here returns 0 and 32, which is only half right. Why does this not work?
#include<stdio.h>
void main()
{
unsigned long long number=576460752303423489;
int i;
for (i=0; i<64; i++)
{
if ((number & (1 << i))==1)
{
printf("%d ",i);
}
}
}
There are two bugs on the line
if ((number & (1 << i))==1)
which should read
if (number & (1ull << i))
Changing 1
to 1ull
means that the left shift is done on a value of type unsigned long long
rather than int
, and therefore the bitmask can actually reach positions 32 through 63. Removing the comparison to 1 is because the result of number & mask
(where mask
has only one bit set) is either mask
or 0
, and mask
is only equal to 1 when i is 0.
However, when I make that change, the output for me is 0 59
, which still isn't what you expected. The remaining problem is that 576460752303423489 (decimal) = 0800 0000 0000 0001
(hexadecimal). 0 59
is the correct output for that number. The number you wanted is 9223372036854775809 (decimal) = 8000 0000 0000 0001
(hex).
Incidentally, main
is required to return int
, not void
, and needs an explicit return 0;
as its last action (unless you are doing something more sophisticated with the return code). Yes, C99 lets you omit that. Do it anyway.
Because (1 << i)
is a 32-bit int
value on the platform you are compiling and running on. This then gets sign-extended to 64 bits for the &
operation with the number
value, resulting in bit 31 being duplicated into bits 32 through 63.
Also, you are comparing the result of the &
to 1, which isn't correct. It will not be 0 if the bit is set, but it won't be 1.
Shifting a 32-bit int by 32 is undefined.
Also, your input number is incorrect. The bits set are at positions 0 and 59 (or 1 and 60 if you prefer to count starting at 1).
The fix is to use (1ull << i), or otherwise to right-shift the original value and &
it with 1 (instead of left-shifting 1). And of course if you do left-shift 1 and &
it with the original value, the result won't be 1 (except for bit 0), so you need to compare != 0
rather than == 1
.
#include<stdio.h>
int main()
{
unsigned long long number = 576460752303423489;
int i;
for (i=0; i<64; i++)
{
if ((number & (1ULL << i))) //here
{
printf("%d ",i);
}
}
}
First is to use 1ULL
to represent unsigned long long
constant. Second is in the if
statement, what you mean is not to compare with 1
, that will only be true for the rightmost bit.
Output: 0 59
It's correct because 576460752303423489
is equal to 0x800000000000001
The problem could have been avoided in the first place by adopting the methodology of applying the >>
operator to a variable, instead of a literal:
if ((variable >> other_variable) & 1)
...
I know the question has some time and multiple correct answers while my should be a comment, but is a bit too long for it. I advice you to encapsulate bit checking logic in a macro and don't use 64 number directly, but rather calculate it. Take a look here for quite comprehensive source of bit manipulation hacks.
#include<stdio.h>
#include<limits.h>
#define CHECK_BIT(var,pos) ((var) & (1ULL<<(pos)))
int main(void)
{
unsigned long long number=576460752303423489;
int pos=sizeof(unsigned long long)*CHAR_BIT-1;
while((pos--)>=0) {
if(CHECK_BIT(number,pos))
printf("%d ",pos);
}
return(0);
}
Rather than resorting to bit manipulation, one can use compiler facilities to perform bit analysis tasks in the most efficient manner (using only a single CPU instruction in many cases).
For example, gcc and clang provide those handy routines:
__builtin_popcountll() - number of bits set in the 64b value
__builtin_clzll() - number of leading zeroes in the 64b value
__builtin_ctzll() - number of trailing zeroes in the 64b value
__builtin_ffsll() - bit index of least significant set bit in the 64b value
Other compilers have similar mechanisms.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.