473,397 Members | 1,972 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,397 software developers and data experts.

position and number of 1's in an integer

Can anyone suggest some efficient way to find the
number of 1's as well as their position in a 32 bit integer ?
Nov 14 '05 #1
6 1481

"junky_fellow" <ju**********@yahoo.co.in> wrote in message
news:8c**************************@posting.google.c om...
Can anyone suggest some efficient way to find the
number of 1's as well as their position in a 32 bit integer ?


1)you need to look at specifying a value with bit e.g. 0x1
2)you will need to use the bit shifting operators << or >>
3)you will need to use logical operators to determine if bits are set e.g. &

these plus a for loop will give you your answer!

Allan
Nov 14 '05 #2
junky_fellow wrote:
Can anyone suggest some efficient way to find the
number of 1's as well as their position in a 32 bit integer ?


Counting the 1-bits is easy enough. Among the several
approaches are loops with masks and shifts, table-based
methods, "horizontal addition," and hybrids of these. Take
your pick.

As for "their position" -- well, it seems to me the
original 32-bit integer is a pretty efficient expression
of the positions of the 1-bits. What other expression did
you have in mind?

You may also be interested in Sean Eron Anderson's
collection of "Bit Twiddling Hacks"

http://graphics.stanford.edu/~seander/bithacks.html

Be warned that some of the tricks described there are not
entirely portable.

--
Eric Sosman
es*****@acm-dot-org.invalid
Nov 14 '05 #3
"puzzlecracker" <ir*********@gmail.com> wrote:
http://graphics.stanford.edu/~seander/bithacks.html


This is amazing!


Yes, it is. Amazingly non-portable, amazingly undependable, amazingly
unlikely to be more efficient than normal, legible code on all systems,
amazingly likely to have already been adopted by the implementation's
optimiser where appropriate, amazingly likely to be used by amateur
programmers to slow down their program and make it unmaintainable where
inappropriate.

In short, sane programmers do not use such ugly hacks unless absolutely
necessary.

Richard
Nov 14 '05 #5
What about this solution junky ?.

int main()
{
int a,i=0,c=0;
printf ("Enter a : ");
scanf("%d",&a);
while(i++<32)
{
if(a&1)
{
c++;
printf ("%d ",i);
}
a=a>>1;
}
printf ("\nNo of 1's : %d\n\n",c);
}

Nov 14 '05 #6
I use this function to count bits in a 32 bit int on both little and
big endian platforms. It's fast, at least on pentium&co. It's written
in such a way that you can easily convert it to a macro if you like.

Ulf Ekström

/* (C) Donald W. Gillies, 1992. All rights reserved. You may reuse
this bitcount() function anywhere you please as long as you retain
this Copyright Notice. */
int bitcount(unsigned int n)
{
register unsigned int tmp;
return (tmp = (n) - (((n) >> 1) & 033333333333) - (((n) >> 2) &
011111111111),
tmp = ((tmp + (tmp >> 3)) & 030707070707),
tmp = (tmp + (tmp >> 6)),
tmp = (tmp + (tmp >> 12) + (tmp >> 24)) & 077);
}

Nov 14 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

24
by: Jeff Johnson | last post by:
Hi All, I'm setting a <div> on a page on a lengthy page with a great deal of text. I want the div to land where the user can see it. I need to capture the vertical scrollbar position and use...
8
by: Jaime Rios | last post by:
Hi, I created a COM AddIn for Word that performs the functions that it needs to, but I needed to add the ability for the toolbar created by the COM AddIn to remember it's last position and...
2
by: AP | last post by:
I am trying to find the integer value for the cursor position in a text box, in particular I want to find out if I am at the end of some text that I typed or at the beginning. I am using IE 6. ...
19
by: Scott Kelley | last post by:
From a byte containing only 1 bit, I need to find the number representing the bit position. Example: 00010000 = 4 00000001 = 0 Is there a simpler method than looping? Thanks,
6
by: meh | last post by:
I can figure out the total number of nodes in a given tree but what I'd like to know is what is the Selected Nodes relationship to the entire tree i.e This is node n out of nnn nodes. In most of...
6
by: Colin McGuire | last post by:
Hello experts, this is a repost but I have been (much) more clear. I want to know the position of the horizontal scrollbar in a textbox as a percentage - if the horizontal scrollbar is hard...
3
by: superjacent | last post by:
Hope someone can help. I have a listbox displaying time periods in blocks of 15 mins for a 24 hour period, all up 96 rows. The listbox can only visibly show 20 rows a time. The default...
30
by: mmgarvey | last post by:
Hi, I'm using python to develop some proof-of-concept code for a cryptographic application. My code makes extended use of python's native bignum capabilities. In many cryptographic...
1
by: spynx | last post by:
import javax.swing.JOptionPane; import java.io.*; import java.util.Arrays; public class election5 { public static void main( String args ) {
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: 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: 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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.