Bitwise And

This is a discussion on Bitwise And within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> int display_binary(unsigned short number); int main() { unsigned short number; printf("Enter a whole number between 0 and ...

  1. #1
    C Newbie
    Join Date
    Oct 2011
    Posts
    59

    Bitwise And

    Code:
    #include <stdio.h>
    
    int display_binary(unsigned short number);
    
    
    int main()
    {
        unsigned short number;
    
    
        printf("Enter a whole number between 0 and 65,535: ");
        scanf("%hu", &number);
    
    
        printf("\n\n    Decimal: %hu\n", number);
        printf("Hexadecimal: %x\n", number);
    
    
        display_binary(number);
    
    
        getch();
        return 0;
    }
    
    
    int display_binary(unsigned short number)
    {
        unsigned short mask = 0x8000;
        int c, k;
    
    
        for(c = 0; c < 16; c++)
        {
            k = (number & mask);
            if(k == 0)
                printf("0");
            else
                printf("1");
    
    
            mask >> 1;
    
    
            if(c == 3 || c == 7 || c == 11)
                printf(" ");
        }
    }
    OK, I know that I am not supposed to just blatantly ask questions for homework, but I can't seem to figure this out... Here is what I was told to do:
    Hint: Declare an unsigned short mask = 0x8000 (which is 1000 0000 0000 0000). Write a for loop that iterates 16 times. In each cycle, do a bitwise & between the mask and the input parameter. Display 0 if the outcome is 0 and 1 otherwise. Shift the mask to the right one bit at the end of each cycle to retrieve the next bit in the next cycle. Display a space character after every 4 bits of data to improve readability.
    My problem is, that I am getting the binary to be all "1's" for everything I put in. I think I am doing what is instructed, but obviously not.

    Thanks in advance for any help,
    ~Alan

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,555
    Code:
    bitwise.c: In function ‘main’:
    bitwise.c:22: warning: implicit declaration of function ‘getch’
    bitwise.c: In function ‘display_binary’:
    bitwise.c:48: warning: control reaches end of non-void function
    getch is not portable, use getchar instead. Also, make display_binary return void, since you don't actually need to return anything.

    Your problem is from the following line:
    Code:
    mask >> 1;
    That doesn't actually do anything. That shifts mask to the right 1, but doesn't store the result anywhere, it's like saying x + 3; x doesn't change, nor mask in your case. It's causing your program to only look at the leftmost bit every time through the loop. Try:
    Code:
     mask >>= 1;

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Line 42... mask = mask >> 1; or simply mask /= 2;

    line 13... you should add some range checking here to make sure you don't get negative numbers or numbers > 65535.



    edit: I see anduril beat me to it
    Last edited by CommonTater; 11-22-2011 at 11:01 AM.

  4. #4
    C Newbie
    Join Date
    Oct 2011
    Posts
    59
    Quote Originally Posted by anduril462 View Post
    Code:
    bitwise.c: In function ‘main’:
    bitwise.c:22: warning: implicit declaration of function ‘getch’
    bitwise.c: In function ‘display_binary’:
    bitwise.c:48: warning: control reaches end of non-void function
    getch is not portable, use getchar instead. Also, make display_binary return void, since you don't actually need to return anything.

    Your problem is from the following line:
    Code:
    mask >> 1;
    That doesn't actually do anything. That shifts mask to the right 1, but doesn't store the result anywhere, it's like saying x + 3; x doesn't change, nor mask in your case. It's causing your program to only look at the leftmost bit every time through the loop. Try:
    Code:
     mask >>= 1;
    Thanks, that did it.

    On a side note, what's the difference between getch and getchar? I'm not exactly sure how being 'portable' affects the outcome.

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Alan Gott View Post
    On a side note, what's the difference between getch and getchar? I'm not exactly sure how being 'portable' affects the outcome.
    getchar() is a standard function sanctioned by the C-99 standard, getch() is not. What this means is that one cannot reasonably expect to find getch() in every compiler's library, making your source code uncompilable on some systems. For example: I use Pelles C, which has it but in a different header, so your code does not compile for me *as is*, I had to add #include <conio.h> to make it work.

  6. #6
    C Newbie
    Join Date
    Oct 2011
    Posts
    59
    Thanks! That makes sense.

  7. #7
    C Newbie
    Join Date
    Oct 2011
    Posts
    59
    Quote Originally Posted by CommonTater View Post
    line 13... you should add some range checking here to make sure you don't get negative numbers or numbers > 65535.
    Yeh, I figured that was needed. But my lab instructor frowns upon extra work. lol

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Alan Gott View Post
    Yeh, I figured that was needed. But my lab instructor frowns upon extra work. lol
    Any teacher who would discourage initiative in his students, should not be teaching.

  9. #9
    C Newbie
    Join Date
    Oct 2011
    Posts
    59
    Quote Originally Posted by CommonTater View Post
    Any teacher who would discourage initiative in his students, should not be teaching.
    She claims that it's a waste of time since it isn't needed for the grade, it's probably because I turned my lab on switch statements from 3 to 15 cases to make it more detailed, and she has to read through everything before grading it. lol

    Anyways, I have a second question. I am supposed to do this:
    The 3 leftmost bits represent the command, the lowest 4bits represent the frequency, and the 8th bit from the right, represents a switch value (on /
    off). Your program should also display the input number in binary to verify the extracted
    information.
    Example: If the input number is 41851, which is 1010 0011 0111 1011 in binary.
    The command value is 101 or 5 in decimal, the frequency is 1011 or 11 in decimal, and
    the 8th bit from the right is 0, which means switch is off.
    The only problem is, that my function "display_binary" can't return anything (as she say's not to in the instructions), and the frequency, switch and command are supposed to be extracted and displayed from the main function.

    So, I can't return the values from the display_binary function, and I can't simply display them from that function either... Any suggestions?

  10. #10
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,555
    Extracted and displayed in the same function? If not, create new functions to get each of those values, otherwise, pass in pointers to arguments and send back the values that way:
    Code:
    void display_binary(unsigned short number, unsigned short *command, unsigned short *frequency, unsigned short *switch)
    {
        ...
        *command = // some bitwise operations to extract the command portion
        // likewise for frequency and switch
        ...
    }

  11. #11
    C Newbie
    Join Date
    Oct 2011
    Posts
    59
    Quote Originally Posted by anduril462 View Post
    Extracted and displayed in the same function? If not, create new functions to get each of those values, otherwise, pass in pointers to arguments and send back the values that way:
    Code:
    void display_binary(unsigned short number, unsigned short *command, unsigned short *frequency, unsigned short *switch)
    {
        ...
        *command = // some bitwise operations to extract the command portion
        // likewise for frequency and switch
        ...
    }
    Like, after that function displays the binary I can't use it anymore. I have to somehow get the information I need while only being able to write in the main function.
    Code:
    int main()
    {
    ...
    display_binary(number);
    //code for extracting/displaying the desired information goes here.
    ...
    }

  12. #12
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,555
    To extract just a set of bits, you need to first mask off the bits you don't want, using bitwise &, then shift them over so they're all the way to the right. Read this: http://www.cprogramming.com/tutorial...operators.html.

  13. #13
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Alan Gott View Post
    She claims that it's a waste of time since it isn't needed for the grade, it's probably because I turned my lab on switch statements from 3 to 15 cases to make it more detailed, and she has to read through everything before grading it. lol
    One has to wonder what some of these screwball teachers are thinking...
    Is she teaching you how to program... or how to get good grades?

    Anyways, I have a second question. I am supposed to do this:


    The only problem is, that my function "display_binary" can't return anything (as she say's not to in the instructions), and the frequency, switch and command are supposed to be extracted and displayed from the main function.

    So, I can't return the values from the display_binary function, and I can't simply display them from that function either... Any suggestions?
    That should be no problem...
    For example:
    Code:
    frequency = number & 0xF;  // keep only 4 lsb
    // etc.

  14. #14
    C Newbie
    Join Date
    Oct 2011
    Posts
    59
    Quote Originally Posted by anduril462 View Post
    To extract just a set of bits, you need to first mask off the bits you don't want, using bitwise &, then shift them over so they're all the way to the right. Read this: Cprogramming.com - Tutorials - Bitwise Operators and Bit Manipulations in C and C++.
    Wouldn't that involve doing the calculations in display_binary then return them to main? (If not, I'm totally clueless to what you are trying to teach me. :S

    Quote Originally Posted by CommonTater View Post
    That should be no problem...
    For example:
    Code:
    frequency = number & 0xF;  // keep only 4 lsb
    // etc.
    Thanks! That worked wonders! Does & 0xF do exactly what I did in my display_binary function without having to loop and whatnot like I had to do?

  15. #15
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Alan Gott View Post
    Wouldn't that involve doing the calculations in display_binary then return them to main? (If not, I'm totally clueless to what you are trying to teach me. :S



    Thanks! That worked wonders! Does & 0xF do exactly what I did in my display_binary function without having to loop and whatnot like I had to do?
    No...

    You had to do all that looping to display the binary value underlying your variable.

    Bitwise and ( & ) operates directly on that underlying value.

    Do a little reading on the logic functions AND OR NEG and XOR for a better insight.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with bitwise AND
    By tehjojo in forum C Programming
    Replies: 1
    Last Post: 04-03-2010, 02:23 PM
  2. Bitwise 'and' vs '=='
    By Ducky in forum Windows Programming
    Replies: 4
    Last Post: 07-27-2009, 12:37 PM
  3. 64 Bitwise AND
    By Lostsheep in forum C++ Programming
    Replies: 2
    Last Post: 06-16-2008, 12:17 PM
  4. Bitwise OR
    By tinkerbell20 in forum C++ Programming
    Replies: 4
    Last Post: 06-11-2005, 02:23 AM
  5. bitwise help
    By linuxdude in forum C Programming
    Replies: 8
    Last Post: 11-20-2003, 07:37 PM

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