Bitwise operators

This is a discussion on Bitwise operators within the C Programming forums, part of the General Programming Boards category; Hello. I'm having a difficult time understanding bitwise operators, specifically & (and) and | (or). How does one actually code ...

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    5

    Bitwise operators

    Hello.

    I'm having a difficult time understanding bitwise operators, specifically & (and) and | (or). How does one actually code it? This is how I've been coding them until now, but when I work it out by hand, I get a different answer than the one the executable is giving me. Furthermore, I checked a bitwise operator calculator online which also gives me the same answer I get by hand. For example, I'm taking two variables and declaring them: A = 5 and B = 8.

    Code:
    printf("The bitwise operation & result for A and B is: ", A & B);
    It gives me the wrong answer when I run it. I'm assuming I'm coding it wrong. Can someone help me?

    Thanks!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,773
    What answer did it give and what did you expect?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    May 2011
    Posts
    5
    Quote Originally Posted by laserlight View Post
    What answer did it give and what did you expect?
    For example, for the | (or) operator, when I ran the executable, it gave me 15. However, when I calculate it by hand and do it with the bitwise online calculator, I get 13.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,773
    Testing with this program, I am afraid that I cannot duplicate the problem:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        int A = 5;
        int B = 8;
        printf("The bitwise operation | result for A and B is: %d\n", A | B);
        return 0;
    }
    I suspect that you merely forgot the format specifier in the printf.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    May 2011
    Posts
    5
    Quote Originally Posted by laserlight View Post
    Testing with this program, I am afraid that I cannot duplicate the problem:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        int A = 5;
        int B = 8;
        printf("The bitwise operation | result for A and B is: %d\n", A | B);
        return 0;
    }
    I suspect that you merely forgot the format specifier in the printf.
    Your code worked all right when I executed it. However, the code I had written was formatted the same way. Also, I had several other operations occurring within the executable (such as addition, subtraction, etc.) and I noticed that when I placed the & and | operators all the way at the end, the error occurred. When I placed it at the top, it worked correctly. Does that have to do something with it?

    Also, thanks for the help.
    Last edited by shethecoder; 05-30-2011 at 09:48 PM.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,773
    Quote Originally Posted by shethecoder
    Also, I had several other operations occurring within the executable (such as addition, subtraction, etc.) and I noticed that when I placed the & and | operators all the way at the end, the error occurred. When I placed it at the top, it worked correctly. Does that have to do something with it?
    That is a rather vague description: it could well be that there was a change to the value of A or B, or there could be a precedence issue, etc. Post the code.

    Quote Originally Posted by shethecoder
    Also, thanks for the help.
    You're welcome
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    May 2011
    Posts
    5
    Quote Originally Posted by laserlight View Post
    That is a rather vague description: it could well be that there was a change to the value of A or B, or there could be a precedence issue, etc. Post the code.


    You're welcome
    I looked over the code a couple of more times and found the issue. I had used the preincrement/postincrement operators beforehand, so it was no surprise that the values had changed.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. need help using bitwise operators
    By alexwink in forum C Programming
    Replies: 12
    Last Post: 11-08-2006, 02:36 PM
  2. bitwise operators?
    By EvilPickles in forum C++ Programming
    Replies: 18
    Last Post: 08-07-2006, 08:53 AM
  3. bitwise operators??!?
    By Mr_Jack in forum C++ Programming
    Replies: 6
    Last Post: 04-20-2004, 08:05 AM
  4. Bitwise operators
    By Unregistered in forum C Programming
    Replies: 22
    Last Post: 03-24-2004, 10:03 AM
  5. Bitwise operators
    By kerrywolfe in forum C++ Programming
    Replies: 2
    Last Post: 02-09-2002, 04:57 PM

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