Simple Bit Manipulation Program

This is a discussion on Simple Bit Manipulation Program within the C Programming forums, part of the General Programming Boards category; Sample of what output is supposed to look like: http://img156.imageshack.us/my.php?image=scan1pp0.gif Basically, I'm supposed to write a program to continually take ...

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    3

    Simple Bit Manipulation Program

    Sample of what output is supposed to look like:
    http://img156.imageshack.us/my.php?image=scan1pp0.gif

    Basically, I'm supposed to write a program to continually take a set of inputted binary numbers, while displaying the inputted sums in decimal, hex and binary.

    Here is a flowchart of how the program is supposed to be done:
    http://img165.imageshack.us/my.php?image=scan2ib8.gif

    I'm having trouble getting the inputted numbers (which are characters) to be placed into a variable as a real number.

    So far this is what I have:
    Code:
    #include "stdafx.h"
    #include "stdio.h"
    
    int main(int argc, char* argv[]) {
    
        unsigned char c, b, n;
        int sum, done;
        sum=0;
        done=0;
        int i=8;
    
        n=0;
        printf("Enter a binary number:");
        c=getchar();
    
        if (c=='q'||c=='Q') {
            getchar();
            return 0;
        } else {
            for ( int x = 0; x < 8; x++ ) {
                b=c&1;
                n=n<<1;
                n=b|1;
            }
            sum=sum+n;
            
            printf("sum = &#37;d decimal %x hex and " , sum, sum);
        }
    
        getchar();
        return 0;
    }

    In the second .gif I linked to, it states that after I get the inputted characters into c, I'm supposed to make b= LSB(Least significant bit) of 'c' and then Shift 'n' left 1 bit and insert 'b' into 'n' as the new LSB

    I just don't understand how to write the code so that it would continually scan until it has all of the inputted numbers into a variable as a real set of bits.

    I thank you for your time and any advice anyone can give.
    Last edited by soosha; 10-28-2007 at 02:18 PM. Reason: formatted the code more

  2. #2
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    This:
    Code:
    for(;;);
    Is an infinite loop with no way to get out of it. You don't want to be doing that.

    It would probably be easiest to read in the binary characters as a string using fgets(), then looping through each character in your string checking if its zero or not.

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    3
    I'm not allowed to use commands I haven't been taught yet
    The Professor wants me to do it only by bit manipulation, getchar and putchar.

  4. #4
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    Code:
    #include "stdafx.h"
    #include "stdio.h"
    
    int main(int argc, char* argv[]) {
    
        unsigned char c, b, n;
        int sum, done;
        sum=0;
        done=0;
        int i=8;
    
        n=0;
        printf("Enter a binary number:");
        c=getchar();
    
        if (c=='q'||c=='Q') {
            getchar();
            return 0;
        } else {
            for ( int x = 0; x < 8; x++ ) {
                b=c&1;
                n=n<<1;
                n=b|1;
            }
            sum=sum+n;
            
            printf("sum = &#37;d decimal %x hex and " , sum, sum);
        }
    
        getchar();
        return 0;
    }
    Formated your code so I could read it, then, changed the for(;; to getchar();. Your intent with the for(;;s was to pause the program right? Well pausing for user input is a lot healthier of a way of doing that, and since you are allowed to use getchar(), why not !

    As to your input issues, you are only reading one character, and assuming you follow what your program told you to do, you will get either a '0' or a '1' (which, btw are the ascii symbols 0 and 1, the value stored in the char isn't actually the number 0 or 1) If you want to read more characters, you are going to have to loop, that is, if you have to use getchar().

  5. #5
    Registered User
    Join Date
    Oct 2007
    Posts
    3

    As to your input issues, you are only reading one character, and assuming you follow what your program told you to do, you will get either a '0' or a '1' (which, btw are the ascii symbols 0 and 1, the value stored in the char isn't actually the number 0 or 1) If you want to read more characters, you are going to have to loop, that is, if you have to use getchar().
    EDIT:
    I appreciate the tips/help. How would I write the loop so that it would only require the user to input 1 set of numbers? I'm not sure how that would work if I would only use getchar() which only stores a single character.

    If I insert those scanned characters into a variable that is declared as an integer, would it then count as the actual numbers of 1 and 0?

    Thanks again.
    Last edited by soosha; 10-28-2007 at 02:39 PM.

  6. #6
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    Perhaps do the input and calculations within your loop. Something like:
    Code:
    for(int x = 0; x< 8; x++) 
    {
        c=getchar();
        c-='0';
        n=n<<1;
        n=c|1;
    }
    With that example any non zero character would be counted as a 1 so you might want to add some validation too.

    [edit] BTW declaring an int in your for loop C++ only, not valid C [/edit]

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by mike_g View Post
    Code:
    for(int x = 0; x< 8; x++) 
    {
        c=getchar();
        c-='0';
        n=n<<1;
        n=c|1;
    }
    To Soosha: The blue code above doesn't look right to me. What is is supposed to do? [I know what it DOES, but that's probably not what you actually wanted... ]

    You may also want to CHECK that the input is valid - e.g. '0' or '1' - otherwise the above "math" will produce "interesting" results.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    Oh yeah >_< you want to be 'anding', not 'oring' there.

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by mike_g View Post
    Oh yeah >_< you want to be 'anding', not 'oring' there.
    Not sure about that - but I'm sure we can agree that the "current" code is broken. [Actually, there is an argument for using and, but if we go that route, we could actually remove another statement - I'll leave it to others to figure out which].

    Since this is almost certainly a "homework" question, I'm reluctant to provide specific code.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  2. Replies: 1
    Last Post: 12-30-2007, 10:08 AM
  3. Help with a bit manipulation program.
    By torqueywrench in forum C Programming
    Replies: 25
    Last Post: 10-25-2005, 03:07 AM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 04:00 PM
  5. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21