Logical Operations on Bit Patterns

This is a discussion on Logical Operations on Bit Patterns within the C++ Programming forums, part of the General Programming Boards category; I'm reading a book I got from the library to learn C++ and I don't understand this. ~ This is ...

  1. #1
    I eat babies
    Join Date
    Jan 2008
    Location
    la-la-land
    Posts
    31

    Logical Operations on Bit Patterns

    I'm reading a book I got from the library to learn C++ and I don't understand this.
    ~ This is the bitwise complement operator. This is a unary operator that will invert the bits in its operand, so 1 becomes 0 and 0 becomes 1.

    & This is the bitwise AND operator, which will AND the corresponding bits in its ioerands. If the corresponding bits are both 1, then the result bit is 1. Otherwise it is 0.

    ^ This is the bitwise exclusive OR operator, which will exclusive-OR the corresponding bits in its operands. If the corresponding bits are different, i.e. one is 1 and the other is 0, then the resulting bit is 1. If the corresponding bits are the same, the resulting bit is 0.

    | This is the bitwise OR operator, which will OR the corresponding bits in its operands. If either of the two corresponding bits is 1, then the result is 1. If both bits are 0, then the result is 0.
    That is the exact quote from the book and I don't understand when you would use them and why you would need too and really what they do. Someone please explain it for a beginner.
    There are only 10 types of people, those who understand binary and those who don't
    C++ Beginner
    Visual C++ 2008 Express Edition!
    Windows XP
    http://img369.imageshack.us/img369/4372/1399mf9.jpg

  2. #2
    Registered User
    Join Date
    Feb 2008
    Posts
    10
    Basically, all of them, except ~, compare 2 bits and return a value, either 0 or 1.

    ~ Just inverts a bit. So ~1 = 0 and ~0 = 1. Think of it as "not true = false" and "not false = true"

    There are many uses for bitwise operators (your computer wouldnt work without them) such as bit masking or calculating Two's complement

    Read the wikipedia article on bitwise operators for a more detailed explanation.

  3. #3
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,145
    do you not understand what they do or how they can be used?

  4. #4
    I eat babies
    Join Date
    Jan 2008
    Location
    la-la-land
    Posts
    31
    Herz explained what they do but I don't understand how they can be used still
    There are only 10 types of people, those who understand binary and those who don't
    C++ Beginner
    Visual C++ 2008 Express Edition!
    Windows XP
    http://img369.imageshack.us/img369/4372/1399mf9.jpg

  5. #5
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    You will use them when you need to extract or set individual bits of the byte

    For example if you want to check that first 3 bits of the byte are 0
    You will use & with mask where these bits are set to 1 and others to 0 to "remove" bits that you are not interested in
    And when compare result to 0
    Code:
    if (x & 0x7 == 0)
    {
       // 3 left bits are zeroes
    }
    This operations are very usefull in networking for example, because most network protocols operate with bit-streams (not byte streams)

    You can for example read the description of RTP-header.

    Or some media stream description - take H264 format and see it by yourself
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    For an example, look up all the flags in std::ios_base::flags. I blatantly stole this example from cplusplus.com:
    Code:
    // modify flags
    #include <iostream>
    using namespace std;
    
    int main () {
      cout.flags ( ios::right | ios::hex | ios::showbase );
      cout.width (10);
      cout << 100;
      return 0;
    }
    Each of the flags stands for integer with one binary digit turned "on" (or at least we can believe so, even though the details are hidden inside the implementation). If we | those numbers together, we get a new integer with a 1 wherever there was a 1 in any of the original things, so that integer basically carries all three of our flags.

    If we want to know whether a flag was set, we can use & with that flag (if both are 1 in the same spot, we'll get a 1 back; if either the flag or the status has a 0 somewhere, we'll get a 0).

  7. #7
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,145
    as for why not use booleans instead, it takes 1/8 the space (compression). A bool is 1 bytes (8 bits), and only one of them is used, whereas you can use all eight bits using bit flags.

  8. #8
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Also consider passing things to functions. Would you rather use a function that required you to pass 32 separate bools or 1 unsigned?
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 32 bit or 64 bit allignment ?! gcc options??
    By mynickmynick in forum C Programming
    Replies: 3
    Last Post: 07-29-2008, 02:43 AM
  2. bit level permutation function
    By zxcv in forum C Programming
    Replies: 2
    Last Post: 07-27-2008, 01:26 PM
  3. A Question About Unit Testing
    By Tonto in forum C++ Programming
    Replies: 2
    Last Post: 12-14-2006, 07:22 PM
  4. bit patterns of negtive numbers?
    By chunlee in forum C Programming
    Replies: 4
    Last Post: 11-08-2004, 07:20 AM
  5. Array of boolean
    By DMaxJ in forum C++ Programming
    Replies: 11
    Last Post: 10-25-2001, 11:45 PM

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