Hi,

I have got this question: Write function of bitcount() that return the number of bits set to 1 in an unsigned integer.

Here's my try:

#include <stdio.h>
int bitcount(unsigned x)
main()
{
int b;
for (B=0; x!=0; x >>=1)
if (x &01)
btt
return b;
}

When i compile, it keep having this error: declaration syntz error.
Anyone can advise me what's the problem? Thanks

Recommended Answers

All 12 Replies

Use code tags. C is case sensitive so if you declare b than you cant use B. Compile the code and fix the syntax errors.

#include <stdio.h>
int bitcount(unsigned x)
main()
{
int b;
for (B=0; x!=0; x >>=1) <<< Where are these variables declared?
if (x &01)
btt  <<< what the he!! is this??? 
return b;
}

hi,

is it something like this?

#include <stdio.h>

int bitcount(unsigned x);

main()
{
int b;
int x;

for (b=0; x!=0; x >>=1)
{
if (x & 01)
{
btt ;
}
}

return b;
}

#include <stdio.h>

int bitcount(unsigned x);

main()
{
int b;
int x;

for (b=0; x!=0; x >>=1)
{
if (x & 01)
{
btt ; <<< what is this for?
}
}

return b;

Hi,

Sorry...it's b++

#include <stdio.h>

int bitcount(unsigned x);

main()
{
int b;
int x;

for (b=0; x!=0; x >>=1)
{
if (x & 01)
{
btt ; <<< what is this for? (b++)
}
}

return b;

doesn't make any sense. why is b++ inside that if statement?

And what is your question? (Don't just repeat the assignment like you did in your other post). You need to learn how to ask intelligent questions, not just repeat what your teacher asked you to do.

i was trying to add the number of bit count of 1 into it so include b++....
the question is Write function of bitcount() that return the number of bits set to 1 in an unsigned integer.
so b is to count the number of 1 in the binary number

>> the question is ...

No, that is not a question. Questions end in a question-mark ??????? A question often starts with the word "How" or "What" or "Why". "Write function of ..." is a statement, a command, an order. It is telling you want to do, not how to do it. If I tell you to mow the lawn (cut the grass) I am not asking a question but telling you what to do. If you don't know how to mow the lawn then you don't just repeat the order but instead ask something about what you don't understand.

Since I don't have my crystle ball with me today I can't read you mind and know what question(s) you want to ask.

[edit] Here is a short tutorial and example program[/edit]

Hi,

I just starting to pick up this c programming....and i rele dun know how to do it...mostly, i flipped through books to find if there is any example simliar to it and it seem like my edit progam doesn't work at all.......plz...anyone out there can help me on writing a complete n able to work program for my 3 posts? I'm really can't compile as it gets more n more errors......anyone can help me? plz!!!!!! i need serious n urgent help

we all understand your frustrations because everyone was a newbe at one time, but nobody is going to write your porograms for you. Post your code, post some of the errors and ask questions, don't just restate the problem.

Why dont you take the help of the excellent tutorial provided by Mr. Ancient Dragon as an aid in developing your program? If you are expecting someone to give you entire code then that would be err... a wrong assumption.
http://www.crasseux.com/books/ctutorial/Masks.html#Masks

Anyways try to convert the follwing algo to code:

1. Declare a variable "x" and initialize it with the number whose number of bits which are set you want to find.

2. Declare and define a "mask" variable with value 0x00000001 (32 bits with the bit 0 set on Pentium Machine, Small Endian i think is what they call it )

3. Run a loop till the mask variable is not completely 0 ( having value 0x00000000 )

4. AND the variable and the mask variable using & operator and if the resultign value is non zero then it means that the 0th bit of the variable "X" is set ie is 1 and hence increment the value of "bitcount".

5. Left shift the mask variable so that its value is now 0x00000002

6. After teh loop is terminated (when mask becomes 0) print the value of the "bitcount" variable.

If you still cant write a program with the help of the given guidelines then err... god help you.

Best of luck

Hi,

since u stated my b++ is wrong...so how should i amend my program as per above to make it correct? could you olz advsie me ...thanks a million!

commented: go to hell bitch +0
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.