## Beginner C/C++

Moderators: None (Apply to moderate this forum)
Number of posts: 16951

This Forum Only

Calculate the number of 1s in the binary representation Posted by raven129 on 11 Dec 2004 at 3:01 PM
Hi guys,

need some c++ help.

i need to get a value 'int value' from a pervious functions and convert to it's binary equivilent then Calculate the number of 1s in the binary representation. Concatenate these numbers to form a new value.

could some one plz help me, thanks

thanks
Re: Calculate the number of 1s in the binary representation Posted by Chainsaw666 on 11 Dec 2004 at 4:12 PM
I've actually made functions to do this.
Make yourself a handly little binary struct like so (I like structs).

typedef_struct binary_typ
{
int nums_mem[8];
}

This is how to make a conversion of a number to binary.

int converter(binary bin, int num)
{
if(num>=128)
{
bin.nums_mem[7]=1;
num-=128;
}
if(num>=64)
{
bin.nums_mem[6]=1;
num-=64;
}
if(num>=32)
{
bin.nums_mem[5]=1;
num-=32;
}
if(num>=16)
{
bin.nums_mem[4]=1;
num-=16;
}
if(num>=8)
{
bin.nums_mem[3]=1;
num-=8;
}
if(num>=4)
{
bin.nums_mem[2]=1;
num-=4;
}
if(num>=2)
{
bin.nums_mem[1]=1;
num-=2;
}
if(num>=1)
{
bin.nums_mem[0]=1;
num-=1;
}
return(bin);
}

Then you simply make another reader.

int converter(binary bin,int result)
{
int loop;
result=0;
for(loop=0;loop<=7;loop++)
{
if(bin.nums_mem[loop]==1)
{
result++;
}
}
return(result);
}

There is a variety of things you could do with this number, this is just an example.
Re: Calculate the number of 1s in the binary representation Posted by raven129 on 11 Dec 2004 at 10:05 PM
thanks heaps for that chainsaw.....never occured to me that u could do it this way, what i was doing bcame so complicated

thanks again

: I've actually made functions to do this.
: Make yourself a handly little binary struct like so (I like structs).
:
: typedef_struct binary_typ
: {
: int nums_mem[8];
: }
:
: This is how to make a conversion of a number to binary.
:
: int converter(binary bin, int num)
: {
: if(num>=128)
: {
: bin.nums_mem[7]=1;
: num-=128;
: }
: if(num>=64)
: {
: bin.nums_mem[6]=1;
: num-=64;
: }
: if(num>=32)
: {
: bin.nums_mem[5]=1;
: num-=32;
: }
: if(num>=16)
: {
: bin.nums_mem[4]=1;
: num-=16;
: }
: if(num>=8)
: {
: bin.nums_mem[3]=1;
: num-=8;
: }
: if(num>=4)
: {
: bin.nums_mem[2]=1;
: num-=4;
: }
: if(num>=2)
: {
: bin.nums_mem[1]=1;
: num-=2;
: }
: if(num>=1)
: {
: bin.nums_mem[0]=1;
: num-=1;
: }
: return(bin);
: }
:
: Then you simply make another reader.
:
: int converter(binary bin,int result)
: {
: int loop;
: result=0;
: for(loop=0;loop<=7;loop++)
: {
: if(bin.nums_mem[loop]==1)
: {
: result++;
: }
: }
: return(result);
: }
:
: There is a variety of things you could do with this number, this is just an example.
:

Re: Calculate the number of 1s in the binary representation Posted by Lundin on 13 Dec 2004 at 2:47 AM
Here is a speed optimized version:

```#include <stdio.h>

int calculate_1 (unsigned char byte)
{
const unsigned char checktable[16]={0,1,1,2,1,2,2,3,1,2,2,3,2,3,3,4};
return checktable[byte&0x0F] + checktable[byte>>4];
}

int main (void)
{
unsigned char data[5] = {0x01, 0x02, 0x04, 0x0F, 0xAA};  // 11
unsigned char sum=0;
unsigned char i;
for(i=0; i<5; i++)
sum += calculate_1(data[i]);

printf("%d",sum);
getchar();

return 0;
}
```

If you need to use other types than char, typecast them and run the function for each byte:

```  int data = 0xF00F00;
unsigned char sum=0;
unsigned char i;

for(i=0; i<sizeof(data); i++)
{
unsigned char* chp = (unsigned char*)&data;
sum += calculate_1(chp[i]);
}
```

## Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic