Thread: Difference between mod and bitwise And operator

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    83

    Difference between mod and bitwise And operator

    Hi,
    Can someone give me the differences between & and % ?
    I have the following algorithm which limits a random variable to 255 only.

    int nRand = rand();
    nRand %= 255;

    Now , doesn't the last statement have similar effect when replacing it with this.
    nRand &= 255;
    ?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Swoorup
    Can someone give me the differences between & and % ?
    What do those operations do?

    Quote Originally Posted by Swoorup
    I have the following algorithm which limits a random variable to 255 only.

    int nRand = rand();
    nRand %= 255;
    Actually, that would set 254 to be the upper limit, but it presumably is a typo and you wanted to write 256.

    Quote Originally Posted by Swoorup
    Now , doesn't the last statement have similar effect when replacing it with this.
    nRand &= 255;
    Yes (once the typo is corrected), but that is just a coincidence.
    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
    Nov 2011
    Posts
    83
    So, you mean, they are the same
    if I change nRand %= 255 to
    nRand %= 256?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Swoorup
    So, you mean, they are the same
    if I change nRand %= 255 to
    nRand %= 256?
    No, they are not the same. They will just have the same effect of limiting the range of the value.
    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
    Nov 2011
    Posts
    83
    I see, thank you.
    Which one would be the most effective for picking random though?

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Changing x %= 256 into x &= 256 is an optimisation many compilers can do for themselves.

    If you're going to optimise anything, then focus on the whole algorithm (like bubble sort vs. quicksort) where your skills can make a real difference.
    Micro optimisations at the single operator level won't do much for you.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Nov 2011
    Posts
    83
    Hmm, thank you
    Is the opmization of the compiler like this though? when x %= 256 then x &= 255 not x &= 256

    I am learning about the lower-level details of C++ and finding out the relation between assembly and C++ via debugging them. So, minor things like this happened to boggle my mind :P

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Swoorup
    Is the opmization of the compiler like this though? when x %= 256 then x &= 255
    Maybe. It depends on whether the compiler can prove that the result will always be the same. For example, -1 % 256 == -1, but -1 % 255 == 255, assuming two's complement. Now, we know for sure that rand() returns a non-negative integer, but the compiler may or may not be able to determine this, or if it can, it might not have done so. Then, there's the issue of proving that for every non-negative integer i in the range of int, (i % 256) == (i & 255).
    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

  9. #9
    Registered User R41D3N's Avatar
    Join Date
    Mar 2012
    Location
    ...Beyond Infinity, Behind a KeyBoard...
    Posts
    29
    -- reserved for when I come up with 1 --

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bitwise operator question
    By f1gh in forum C Programming
    Replies: 1
    Last Post: 12-25-2010, 12:31 PM
  2. bitwise operator
    By donkey_C in forum C Programming
    Replies: 7
    Last Post: 09-26-2010, 02:47 PM
  3. Bitwise Operator Help
    By ggraz in forum C Programming
    Replies: 2
    Last Post: 09-28-2008, 09:20 AM
  4. what does this mean, bitwise operator
    By InvariantLoop in forum C++ Programming
    Replies: 11
    Last Post: 03-09-2006, 07:43 PM
  5. bitwise operator
    By ssharish2005 in forum C Programming
    Replies: 8
    Last Post: 09-30-2005, 01:17 AM