473,395 Members | 1,468 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Checking how many bits are set in an integer

Hi All,
I want to check how many bits are set in an integer .
For example,

int a = 10;
The binary form of 10 is 1010.
There are two bits set to 1 in the number 10.
Like that i have check how many bits are set to 1 in an integer.
Can anyone help me in this....


Thanks in advance,
Anand.
Sep 15 '08 #1
5 6624
newb16
687 512MB
http://graphics.stanford.edu/~seander/bithacks.html
Sep 15 '08 #2
I need to find it without using any loops...
Sep 15 '08 #3
JosAH
11,448 Expert 8TB
I need to find it without using any loops...
Did you read the link that was given to you? Have you read about the parallel
counting method?

kind regards,

Jos
Sep 15 '08 #4
I need to find it without using any loops...
You mean a recursive function?
Sep 15 '08 #5
donbock
2,426 Expert 2GB
Hi All,
I want to check how many bits are set in an integer .
For example,

int a = 10;
The binary form of 10 is 1010.
There are two bits set to 1 in the number 10.
Like that i have check how many bits are set to 1 in an integer.
Can anyone help me in this....
I need to find it without using any loops...
Are there any other constraints imposed on this homework problem?

Another approach is divide-and-conquer: recognize that you can break your input value into smaller chunks (bytes, nibbles, or even bits) and that the sum of the number of bits set in all the chunks is the same as the number of bits set in the input value. For a small enough chunk it becomes efficient to use a lookup table. However, you can't break the input value into chunks unless you know how many chunks fit in the input type. Take a look at <limits.h> to see if you can think of a reasonably portable way to do this.

Cheers,
donbock
Sep 15 '08 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: John | last post by:
Hi, I need to write out bits that I receive from another process. These are boolean values. I need there to be 8 bits in every byte. I know I could write these bits out as char's using one bit...
34
by: Cuthbert | last post by:
Hi folks, I am trying to find a more efficient way to count "How many bits are '1' in a integer variable?". I still have no idea to count the bits except using a loop and "if" statements....
1
by: hashem | last post by:
Convert a 4 digit number stored in a global array bcdval in the format of Binary Coded Decimal (BCD) to a 16‐bit integer value stored in global variable ival. Sample of input and output: char bcdval...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.