IT Community - Software Programming, Web Development and Technical Support

counting the number of bits

This is a discussion on counting the number of bits within the C and C++ Programming forums, part of the Software Development category; Hi, Can anyone tell what is the most efficient way to count the number of bits which are set in ...


Go Back   IT Community - Software Programming, Web Development and Technical Support > Software Development > C and C++ Programming

Register FAQ Members List Calendar Mark Forums Read
  #1 (permalink)  
Old 07-19-2007, 12:14 AM
prasath prasath is offline
D-Web Sr.Programmer
 
Join Date: Jul 2007
Location: Chennai
Posts: 173
prasath is on a distinguished road
Smile counting the number of bits

Hi,

Can anyone tell what is the most efficient way to count the number of bits which are set in a value?

Thanks,
Prasath.K

Last edited by Booom : 08-18-2007 at 01:24 AM.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 08-17-2007, 11:47 PM
Venkat Venkat is offline
D-Web Master
 
Join Date: Mar 2007
Posts: 350
Venkat is on a distinguished road
Thumbs up Re: What is the most efficient way to count the number of bits which are set in a val

hey,


foo(unsigned char *ByteField)
{
int i=0,j=0;
unsigned char *BytePos;
short Bit;

BytePos = ByteField + sizeof(ByteField);

for (i=0; i
{
if (*(BytePos - i) > (char ) 0)
{
for (j=0; j<8; j++)
{

if (((*(BytePos - i)) & 1<<
{
Bit = (i * 8) + j;
printf("Bit Set %d n",Bit);
}
}
}
}
}

Should give u the bit which are set
__________________
Venkat
knowledge is Power
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 08-29-2007, 03:52 AM
seesamjagan seesamjagan is offline
D-Web Programmer
 
Join Date: Aug 2007
Location: Chennai
Posts: 66
seesamjagan is on a distinguished road
Send a message via AIM to seesamjagan Send a message via Yahoo to seesamjagan
Thumbs up Re: counting the number of bits

try the followin function, i dint try it yet, but hope it will work

int countBits(int val){
int count=0;
while(val != 0){
if((val>>1) == 1){
count++;
}
val >>= 1;
}
return count;
}
__________________
SeeSamJagan
- Sky is not the "LIMIT", Death is not the END, There is still something beyond....
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 08-29-2007, 04:09 AM
seesamjagan seesamjagan is offline
D-Web Programmer
 
Join Date: Aug 2007
Location: Chennai
Posts: 66
seesamjagan is on a distinguished road
Send a message via AIM to seesamjagan Send a message via Yahoo to seesamjagan
Thumbs up Re: counting the number of bits

Hi,
i just made small mistake in the last post
try the modified function

int countBits(int val){
int count=0;
int len = sizeof(val)*8;
while(len != 0){
if((val>>1) == 1){
count++;
}
val >>= 1;
len--;
}
return count;
}
__________________
SeeSamJagan
- Sky is not the "LIMIT", Death is not the END, There is still something beyond....
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to create random number in c? Murali C and C++ Programming 6 07-23-2008 12:26 AM
Bits and bytes srikumar_l Computer Hardware 0 12-23-2007 03:07 AM
Counting Rec of multiple tables in one query? oxygen Database Support 6 12-06-2007 07:40 AM
To get random number in mysql Murali Database Support 7 11-05-2007 11:34 PM
Hiding first number of characters. S.Vinothkumar Database Support 4 09-28-2007 07:37 AM


All times are GMT -7. The time now is 08:45 PM.


Copyright ©2004 - 2007, DiscussWeb. All Rights Reserved.

SEO by vBSEO 3.0.0