473,396 Members | 2,076 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,396 software developers and data experts.

a fanny question

hi eyeryone
001101000000000000100000000000000 is a number in binary system.how can
i get the number of '1's ?
thanks in advance
Jun 27 '08 #1
20 1951
xinxin wrote:
hi eyeryone
001101000000000000100000000000000 is a number in binary system.how can
i get the number of '1's ?
Easy: just count them. I count 4.

Bye, Jojo
Jun 27 '08 #2
On 22 abr, 11:10, "Joachim Schmitz" <nospam.j...@schmitz-digital.de>
wrote:
xinxin wrote:
hi eyeryone
001101000000000000100000000000000 is a number in binary system.how can
i get the number of '1's ?

Easy: just count them. I count 4.

Bye, Jojo
Are you using a string?
Maybe "for" can help you.
Jun 27 '08 #3
On 4月22日, 下午10时03分, xinxin <jnzhouxin...@gmail..comwrote:
hi eyeryone
001101000000000000100000000000000 is a number in binary system.how can
i get the number of '1's ?
thanks in advance
oh ,i must add something .there is little '1's .and it's not a string
but
a unsinged long type. that's to say i what to know how many '1's
a number in decimal system has when it showed in binary system.anyone
get any idea.
Jun 27 '08 #4
On Apr 22, 10:03 am, xinxin <jnzhouxin...@gmail.comwrote:
hi eyeryone
001101000000000000100000000000000 is a number in binary system.how can
i get the number of '1's ?
thanks in advance
http://graphics.stanford.edu/~seander/bithacks.html
Jun 27 '08 #5
"xinxin" <jn**********@gmail.comwrote in message
news:75**********************************@t54g2000 hsg.googlegroups.com...
hi eyeryone
001101000000000000100000000000000 is a number in binary system.how can
i get the number of '1's ?
thanks in advance
I used this code. But I'm not sure if it's guaranteed to complete.

#include <stdio.h>
#include <stdlib.h>
#include <limits.h>

#define int_bits sizeof(unsigned int)*CHAR_BIT

/* return number of set bits in n */
int countbits(unsigned int n) {
int count,bitno;
unsigned m;

count=0;

while (n) {
bitno = rand() & (int_bits-1); /* assume 2^k bits */
m=1;
if (bitno) m<<=bitno; /* m<<0 legal? don't know */

if (n & m) {
++count;
n &= (~m);
};
};

return count;
}

char* strbits(unsigned int n) {
static char s[int_bits+2];
int i,j;

j=int_bits;
s[int_bits]=0;
for (i=0; i<int_bits; ++i) {
s[--j] = ((n & 1) ?'1':'0');
n>>=1;
};

return s;
}

int main(void)
{
int count,bitno;
unsigned int number=0x55555555;

printf("Number = %u\n",number);
printf(" = %sB\n",strbits(number));
printf("1 Bits = %d\n",countbits(number));

}

--
Bartc
Jun 27 '08 #6
xinxin wrote:

[misleading subject line]
hi eyeryone
001101000000000000100000000000000 is a number in binary system.how can
i get the number of '1's ?
thanks in advance
You can use a combnation of shifts and bit masks to calculate the number
of set bits. Code for this has often been posted to this group. A
Google search might help.

Here is a quick and dirty program I cobbled up. Haven't tested it much.

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <limits.h>

unsigned n_setbits(unsigned long);

int main(int argc, char **argv) {
unsigned long n;

if (argc != 2) {
puts("Usage: program number.");
exit(EXIT_FAILURE);
}
else {
errno = 0;
n = strtoul(argv[1], NULL, 0);
if (errno == ERANGE) {
puts("Conversion error.");
exit(EXIT_FAILURE);
}
printf("Set bits = %u\n", n_setbits(n));
}
return 0;
}

unsigned n_setbits(unsigned long n) {
unsigned setbits, ctr;
unsigned long mask;

for (ctr = setbits = 0, mask = 1;
ctr <= sizeof(unsigned long) * CHAR_BIT;
ctr++, mask <<= 1) {
if (n & mask) {
setbits++;
}
}
return setbits;
}

Jun 27 '08 #7
xinxin wrote:
hi eyeryone
001101000000000000100000000000000 is a number in binary system.how can
i get the number of '1's ?
thanks in advance
Count them.
Jun 27 '08 #8
santosh wrote:
unsigned n_setbits(unsigned long n) {
unsigned setbits, ctr;
unsigned long mask;

for (ctr = setbits = 0, mask = 1;
ctr <= sizeof(unsigned long) * CHAR_BIT;
WHY?!!!

Work with _values_, not _representations_.

The expression sizeof(unsigned long) * CHAR_BIT needn't
match the number of value bits in unsigned long.

The mask you're using will become zero when you run out
of bits. You don't need to count how many bits there might
be when you're already traversing the bits that are.

That said, I generally prefer...

for (mask = -1, mask = mask / 2 + 1; mask; mask <<= 1)

....because it's portable to unsigned types with rank lower
than int. [Note that it's theoretically possible that USHRT_MAX
== INT_MAX.]
ctr++, mask <<= 1) {
if (n & mask) {
setbits++;
}
}
return setbits;
}
--
Peter
Jun 27 '08 #9
On 4月23日, 上午2时03分, santosh <santosh....@gmail.comwrote:
xinxin wrote:

[misleading subject line]
hi eyeryone
001101000000000000100000000000000 is a number in binary system.how can
i get the number of '1's ?
thanks in advance

You can use a combnation of shifts and bit masks to calculate the number
of set bits. Code for this has often been posted to this group. A
Google search might help.
the idea is great. and it work perfectly. but as i said before ,there
is little '1' in the number of binary system. must i get the number of
'1's
it contains(or includes,i don't know which word to choose) by checking
it bit by bit .that's maybe the most fanny as well as most challenging
part
of this small question

Jun 27 '08 #10
xinxin <jn**********@gmail.comwrites:
On 4鏈23鏃, 涓婂崍2鏃03鍒, santosh <santosh....@gmail.comwrote:
>xinxin wrote:

[misleading subject line]
hi eyeryone
001101000000000000100000000000000 is a number in binary system.how can
i get the number of '1's ?
thanks in advance

You can use a combnation of shifts and bit masks to calculate the number
of set bits. Code for this has often been posted to this group. A
Google search might help.
the idea is great. and it work perfectly. but as i said before ,there
is little '1' in the number of binary system. must i get the number of
'1's
it contains(or includes,i don't know which word to choose) by checking
it bit by bit .that's maybe the most fanny as well as most challenging
part
of this small question
I think what you're saying is that you have a binary number with
relatively few 1s, and you'd like a way to count the 1s that's more
efficient than traversing all the bits.

As santosh said, this has been discussed here before. A Google search
will likely turn up a number of solutions. Here's one I just found:
<http://infolab.stanford.edu/~manku/bitcount/bitcount.html(I do not
vouch for the accuracy of the information).

Incidentally, you should look up the word "fanny" in your English
dictionary. I don't know what you're trying to say, but it doesn't
mean what you think it means.
--
Keith Thompson (The_Other_Keith) <ks***@mib.org>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jun 27 '08 #11
Keith Thompson wrote:
>
Incidentally, you should look up the word "fanny" in your English
dictionary. I don't know what you're trying to say, but it doesn't
mean what you think it means.
It doesn't even mean the same in American and British English...

--
Ian Collins.
Jun 27 '08 #12
Sjouke Burry wrote:
xinxin wrote:
>001101000000000000100000000000000 is a number in binary system.
how can i get the number of '1's ?

Count them.
Load the phrase into your text editor. Place the cursor on the
first digit. Record the column number shown by the editor as l.
Move the cursor to just past the last digit. Record the column
number as r. Now count the number of zeroes in the whole number,
and record as m. Then solve the equation:

n + m = r - l

for n. Notice that you can also count 1's and solve for the number
of zeroes. Ain't maths wonderful!

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.

** Posted from http://www.teranews.com **
Jun 27 '08 #13
On 4月23日, 上午9时14分, Keith Thompson <ks...@mib.orgwrote:
xinxin <jnzhouxin...@gmail.comwrites:
On 4月23日, 上午2时03分, santosh <santosh....@gmail.comwrote:
xinxin wrote:
[misleading subject line]
hi eyeryone
001101000000000000100000000000000 is a number in binary system.how can
i get the number of '1's ?
thanks in advance
You can use a combnation of shifts and bit masks to calculate the number
of set bits. Code for this has often been posted to this group. A
Google search might help.
the idea is great. and it work perfectly. but as i said before ,there
is little '1' in the number of binary system. must i get the number of
'1's
it contains(or includes,i don't know which word to choose) by checking
it bit by bit .that's maybe the most fanny as well as most challenging
part
of this small question

I think what you're saying is that you have a binary number with
relatively few 1s, and you'd like a way to count the 1s that's more
efficient than traversing all the bits.

As santosh said, this has been discussed here before. A Google search
will likely turn up a number of solutions. Here's one I just found:
<http://infolab.stanford.edu/~manku/bitcount/bitcount.html(I do not
vouch for the accuracy of the information).

Incidentally, you should look up the word "fanny" in your English
dictionary. I don't know what you're trying to say, but it doesn't
mean what you think it means.

--
Keith Thompson (The_Other_Keith) <ks...@mib.org>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"- 隐藏被引用文字 -

- 显示引用的文字 -
oh, you give me the best solution to my quetion. and maybe i should
learn english harder .english is important for a chinese to learn
software.
Jun 27 '08 #14
On 23 Apr, 01:44, xinxin <jnzhouxin...@gmail.comwrote:
the idea is great. and it work perfectly. but as i said before ,there
is little '1' in the number of binary system.
a "little '1'" is presumably ~0.9 and a "big '1'" ~1.1
:-)
--
Nick keighley
Jun 27 '08 #15
In article <87************@kvetch.smov.org>, Keith Thompson <ks***@mib.orgwrote:
>Incidentally, you should look up the word "fanny" in your English
dictionary. I don't know what you're trying to say, but it doesn't
mean what you think it means.
And, of course, the meaning given in the dictionary will depend on which side
of the Atlantic Ocean the dictionary was printed on. <g>
Jun 27 '08 #16
xinxin wrote:
hi eyeryone
001101000000000000100000000000000 is a number in binary system.how can
i get the number of '1's ?
thanks in advance
long unsigned bit_count(long unsigned n)
{
long unsigned count;

for (count = 0; n != 0; n &= n - 1) {
++count;
}
return count;
}

--
pete
Jun 27 '08 #17
long unsigned bit_count(long unsigned n)
{
* * *long unsigned count;

* * *for (count = 0; n != 0; n &= n - 1) {
* * * * *++count;
* * *}
* * *return count;

}

Or another alternative:

unsigned HighCount(IntType val)
{
unsigned count = 0;

for ( ; val; val >>= 1)
count += (count & (IntType)1);

return count;
}

I could have replaced the body of the loop with:
if (count & (IntType)1) ++count;

but I think an addition would be faster than a branching.
Jun 27 '08 #18
On Apr 24, 5:38*pm, Tom醩 h蒳lidhe <t...@lavabit.comwrote:
long unsigned bit_count(long unsigned n)
{
* * *long unsigned count;
* * *for (count = 0; n != 0; n &= n - 1) {
* * * * *++count;
* * *}
* * *return count;
}

Or another alternative:

unsigned HighCount(IntType val)
{
* * unsigned count = 0;

* * for ( ; val; val >>= 1)
* * * * count += (count & (IntType)1);

* * return count;

}

I could have replaced the body of the loop with:
* * if (count & (IntType)1) ++count;

but I think an addition would be faster than a branching.
This problem arises all the time in game programming. It's typically
called "population count" or "popcount". Here is the Chess
Programming Wiki entry on it:
http://chessprogramming.wikispaces.com/Population+Count
Jun 27 '08 #19
Tom醩 h蒳lidhe wrote:
>long unsigned bit_count(long unsigned n)
{
long unsigned count;

for (count = 0; n != 0; n &= n - 1) {
++count;
}
return count;

}


Or another alternative:

unsigned HighCount(IntType val)
{
unsigned count = 0;

for ( ; val; val >>= 1)
count += (count & (IntType)1);

return count;
}

I could have replaced the body of the loop with:
if (count & (IntType)1) ++count;

but I think an addition would be faster than a branching.
HighCount uses a very different algorithm from bit_count.
The running time of bit_count
is proportional to the return value.

--
pete
Jun 27 '08 #20
In article <bf**********************************@j22g2000hsf. googlegroups.com>
Tom醩 h蒳lidhe <to*@lavabit.comwrote:
unsigned count = 0;
for ( ; val; val >>= 1)
count += (count & (IntType)1);
I am surprised no one else pointed this out yet: since count
is initially 0, (count & 1) is 0, so this always adds 0 to 0,
leaving count 0. So the total count is always 0.

(Obviously you meant:

count += (val & 1);

and the cast to IntType is not needed, so I left it out.)
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (4039.22'N, 11150.29'W) +1 801 277 2603
email: gmail (figure it out) http://web.torek.net/torek/index.html
Jun 27 '08 #21

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

Similar topics

3
by: Stevey | last post by:
I have the following XML file... <?xml version="1.0"?> <animals> <animal> <name>Tiger</name> <questions> <question index="0">true</question> <question index="1">true</question> </questions>
7
by: nospam | last post by:
Ok, 3rd or is it the 4th time I have asked this question on Partial Types, so, since it seems to me that Partial Types is still in the design or development stages at Microsoft, I am going to ask...
3
by: Ekqvist Marko | last post by:
Hi, I have one Access database table including questions and answers. Now I need to give answer id automatically to questionID column. But I don't know how it is best (fastest) to do? table...
10
by: glenn | last post by:
I am use to programming in php and the way session and post vars are past from fields on one page through to the post page automatically where I can get to their values easily to write to a...
10
by: Rider | last post by:
Hi, simple(?) question about asp.net configuration.. I've installed ASP.NET 2.0 QuickStart Sample successfully. But, When I'm first start application the follow message shown. ========= Server...
53
by: Jeff | last post by:
In the function below, can size ever be 0 (zero)? char *clc_strdup(const char * CLC_RESTRICT s) { size_t size; char *p; clc_assert_not_null(clc_strdup, s); size = strlen(s) + 1;
56
by: spibou | last post by:
In the statement "a *= expression" is expression assumed to be parenthesized ? For example if I write "a *= b+c" is this the same as "a = a * (b+c)" or "a = a * b+c" ?
2
by: Allan Ebdrup | last post by:
Hi, I'm trying to render a Matrix question in my ASP.Net 2.0 page, A matrix question is a question where you have several options that can all be rated according to several possible ratings (from...
3
by: Zhang Weiwu | last post by:
Hello! I wrote this: ..required-question p:after { content: "*"; } Corresponding HTML: <div class="required-question"><p>Question Text</p><input /></div> <div...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
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.