# Thread: need help using bitwise operators

1. ## need help using bitwise operators

im writing a program that reads a character from the keyboard and converts it to binary. i have already written the part of the program that converts to binary but now i need to have the program count the number of 1's in the binary using bitwise operators. can anoyone give me a helping hand?

2. Ignoring the clever solutions, just loop and shift until the value is zero. Do an AND with the least significant bit and increment your counter if it's not zero.

3. i dont have a very good understanding of bitwise operators so i'm pretty lost right now

4. Post what you've done and we'll help you. Bitwise OPs are & | ^ >> << and stand for <bitwise preceeds each of these> and, or, xor, right shift, and left shift respectively.

5. >so i'm pretty lost right now
Get used to it. Programmers spend most of their time confused.

6. Originally Posted by Prelude
>so i'm pretty lost right now
Get used to it. Programmers spend most of their time confused.
I don't understand what you mean by that??????

7. Code:
```int howmanyones(char a){
int counter=0;
for(;a>0;a>>=1){
if(a%2==1){
counter++;
}
}
return counter;
}```
This should do it, but I think there's a better way to check if the last bit is 1...
Get used to it. Programmers spend most of their time confused.
You can't avoid being confused. If you know the basics then thinking should solve most of the problems.

8. this is what i have so far
Code:
```#include<stdio.h>
#include<conio.h>
#include<math.h>
int main(){
unsigned char x;
int icount,y;
printf("Enter a number you wish to convert to binary -> ");
scanf("%d",&x);
icount=7;
while(icount>=0){
y=x&(int)pow(2.,icount);
y=y>>icount;
printf("%d",y);
icount--;
}
getch();
return 0;
}```

9. There's no such thing as converting to binary when dealing with integers. It's just how you display it.

10. what do you think the line
>y=x&(int)pow(2.,icount);
does?

To print your numbers, you'd do something like:
printf("%i", (1<<count) | innum);

11. >scanf("%d",&x);
x is a char. It may seem like an innocent thing, but this makes your whole program completely unpredictable. What you should be doing is declaring x as an unsigned int and usign %u.

>what do you think the line
>>y=x&(int)pow(2.,icount);
>does?
Just what I'd expect, the same thing as (1U << icount). Why?

>A better way to /2 however, is >>.
If by "better" you mean more obscure and error prone, sure.

12. Originally Posted by Prelude
>A better way to /2 however, is >>.
If by "better" you mean more obscure and error prone, sure.
If you notice, I deleted that 15 seconds after I wrote it. . . How'd you see that anyways?

EDIT: And, why use pow() when you don't have too? That is such a clunky function anyway.

13. >If you notice, I deleted that 15 seconds after I wrote it. . .
I did notice, but I'm lazy when it comes to editing my own posts.

>How'd you see that anyways?
I'm quick.

>EDIT: And, why use pow() when you don't have too?
It's immediately obvious to someone who isn't really familiar with bitwise operations, I suppose.

Popular pages Recent additions