Thread: need help using bitwise operators

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    8

    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. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    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.
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    8
    i dont have a very good understanding of bitwise operators so i'm pretty lost right now

  4. #4
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    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. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >so i'm pretty lost right now
    Get used to it. Programmers spend most of their time confused.
    My best code is written with the delete key.

  6. #6
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Quote 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. #7
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    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.
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  8. #8
    Registered User
    Join Date
    Oct 2006
    Posts
    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. #9
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    There's no such thing as converting to binary when dealing with integers. It's just how you display it.
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  10. #10
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    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. #11
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >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.
    My best code is written with the delete key.

  12. #12
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Quote 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. #13
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >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.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bitwise operators
    By gnewfenix in forum C Programming
    Replies: 2
    Last Post: 05-16-2009, 08:43 PM
  2. Bitwise Operators
    By rrc55 in forum C Programming
    Replies: 6
    Last Post: 04-30-2009, 11:37 AM
  3. Palindromes and Bitwise operators
    By Dr Tornillo in forum C Programming
    Replies: 8
    Last Post: 08-02-2007, 02:31 PM
  4. bitwise and arithmetic Operators
    By Whiteghost in forum C Programming
    Replies: 4
    Last Post: 12-28-2006, 02:13 PM
  5. Bitwise Operators, Help!!
    By Mini__C in forum C Programming
    Replies: 6
    Last Post: 07-14-2004, 04:20 PM