🔒 Locked
bitwise operations in c
Advertisement
Original Post
I wasn''t asking anyone to do this for me, I was just wondering if anyone had any ideas as to a good way to go about it.
the tricky part about this is that it needs to be done with out the use of "for" or "while" or "if" or "do while"
just bitwise operatiors like & | << >> ! ^ ~
the tricky part about this is that it needs to be done with out the use of "for" or "while" or "if" or "do while"
just bitwise operatiors like & | << >> ! ^ ~
quote:
Original post by samps005
I''m trying to write a program called bitCount -
It returns the of number of 1''s in word (32 bit word size)
Examples: bitCount(5) = 2, bitCount(7) = 3
Legal ops: ! ~ & ^ | + << >>
Max ops: 40
Does anyone think they can do this, or maybe even have some hints
#include <stdio.h>
void main(int argc,char** argv)
{
int num;
int bitcount = 0;
printf("Enter a number: ");
scanf("%i",#);
bitcount += ((num & 0x00000001) >> 0);
bitcount += ((num & 0x00000002) >> 1);
bitcount += ((num & 0x00000004) >> 2);
bitcount += ((num & 0x00000008) >> 3);
bitcount += ((num & 0x00000010) >> 4);
bitcount += ((num & 0x00000020) >> 5);
bitcount += ((num & 0x00000040) >> 6);
bitcount += ((num & 0x00000080) >> 7);
bitcount += ((num & 0x00000100) >> 8);
bitcount += ((num & 0x00000200) >> 9);
bitcount += ((num & 0x00000400) >> 10);
bitcount += ((num & 0x00000800) >> 11);
bitcount += ((num & 0x00001000) >> 12);
bitcount += ((num & 0x00002000) >> 13);
bitcount += ((num & 0x00004000) >> 14);
bitcount += ((num & 0x00008000) >> 15);
bitcount += ((num & 0x00010000) >> 16);
bitcount += ((num & 0x00020000) >> 17);
bitcount += ((num & 0x00040000) >> 18);
bitcount += ((num & 0x00080000) >> 19);
bitcount += ((num & 0x00100000) >> 20);
bitcount += ((num & 0x00200000) >> 21);
bitcount += ((num & 0x00400000) >> 22);
bitcount += ((num & 0x00800000) >> 23);
bitcount += ((num & 0x01000000) >> 24);
bitcount += ((num & 0x02000000) >> 25);
bitcount += ((num & 0x04000000) >> 26);
bitcount += ((num & 0x08000000) >> 27);
bitcount += ((num & 0x10000000) >> 28);
bitcount += ((num & 0x20000000) >> 29);
bitcount += ((num & 0x40000000) >> 30);
bitcount += ((num & 0x80000000) >> 31);
printf("bitcount: %i\n",bitcount);
}
I was feeling nice
Just loop through from 0 to 31 for each bit. The big group of lines can be turned into a for statement with 1 line.
yeah it''s gay alright,
anyway here is how you do it
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
int bitCount(int x) {
{
x = ( x & 0x55555555 ) + ( ( x >> 1 ) & 0x55555555 );
x = ( x & 0x33333333 ) + ( ( x >> 2 ) & 0x33333333 );
x = ( x & 0x0F0F0F0F ) + ( ( x >> 4 ) & 0x0F0F0F0F );
x = ( x & 0x00FF00FF ) + ( ( x >> 8 ) & 0x00FF00FF );
x = ( x & 0x0000FFFF ) + ( ( x >> 16 ) & 0x0000FFFF );
return x;
}
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
anyway here is how you do it
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
int bitCount(int x) {
{
x = ( x & 0x55555555 ) + ( ( x >> 1 ) & 0x55555555 );
x = ( x & 0x33333333 ) + ( ( x >> 2 ) & 0x33333333 );
x = ( x & 0x0F0F0F0F ) + ( ( x >> 4 ) & 0x0F0F0F0F );
x = ( x & 0x00FF00FF ) + ( ( x >> 8 ) & 0x00FF00FF );
x = ( x & 0x0000FFFF ) + ( ( x >> 16 ) & 0x0000FFFF );
return x;
}
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement