Bitwise Operators

This is a discussion on Bitwise Operators within the C++ Programming forums, part of the General Programming Boards category; I'm having trouble with these little buggers. I know how to turn certain bits on, but I can't work out ...

  1. #1
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401

    Bitwise Operators

    I'm having trouble with these little buggers. I know how to turn certain bits on, but I can't work out the operator to turn them off. Eg:

    Code:
    int integer;
    integer=45;
    integer|=1|4|8; //Ensures that these bits are on
    integer?=2|16; //Ensures that these bits are off
    What I want is the symbol where the question mark is. What code can I use to ensure that certain bits are turned off?
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  2. #2
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    Okay, I searched, and I found an article by some self-righteous dude who actually said the right stuff then got the code wrong. This was the article: Bitwise Operators

    For clearing a bit, he had the right theory, but when he showed an example, he must have made a few typos. But this is how I am clearing a bit now:

    Code:
    int integer;
    integer=11;
    integer&=(~8);
    Is this the best way to do it?
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  3. #3
    Registered User
    Join Date
    Jun 2003
    Posts
    70
    For example:

    I have a number 11. So its binary form will be as follows:

    Bits : 0 0 0 0 1 0 1 1
    Bit Number : 7 6 5 4 3 2 1 0

    Now suppose I want to turn off 3th bit.

    So I have to do bitwise AND (&) operation with following Number

    Number(Bits) : 1 1 1 1 0 1 1 1
    Bit Number : 7 6 5 4 3 2 1 0


    So result of two number will be

    0 0 0 0 1 0 1 1 (11)
    & 1 1 1 1 0 1 1 1 (247)
    -----------------------
    0 0 0 0 0 0 1 1 (3)

    So programatically you can do like this:

    {
    unsigned char c1 = 11;
    unsigned char c2 = 247;
    unsigned char c3 = c1 & c2;
    }

    So c3 will be 3.
    Chintan R Naik

  4. #4
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    I don't know, cr_naik, I think Salem's method and my method are a bit easier to use. But hey, do it your way if it makes you happy.
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,799
    You can also try to use the bitset class but it may be overkill depending on what you want to do with it.
    Code:
    #include <bitset>
    #include <limits>
    #include <iostream>
    using namespace std;
    
    int main()
    {
        bitset<numeric_limits<int>::digits> bits(45);
    
        cout << "bits is: " << bits << endl;
    
        bits.set(1);        // Turn on bit 1
        bits.set(4);        // Turn on bit 4
        bits.set(8);        // Turn on bit 8
    
        bits.reset(2);      // Turn off bit 2
        bits.reset(16);     // Turn off bit 16, no effect since it is already 0
    
        cout << "bits is: " << bits << endl;
    
        return 0;
    }
    Outputs:
    bits is: 00000000000000000000000000101101
    bits is: 00000000000000000000000100111011
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bitwise operators
    By gnewfenix in forum C Programming
    Replies: 2
    Last Post: 05-16-2009, 08:43 PM
  2. Bitwise Operators
    By rrc55 in forum C Programming
    Replies: 6
    Last Post: 04-30-2009, 11:37 AM
  3. Palindromes and Bitwise operators
    By Dr Tornillo in forum C Programming
    Replies: 8
    Last Post: 08-02-2007, 02:31 PM
  4. bitwise and arithmetic Operators
    By Whiteghost in forum C Programming
    Replies: 4
    Last Post: 12-28-2006, 01:13 PM
  5. Bitwise Operators, Help!!
    By Mini__C in forum C Programming
    Replies: 6
    Last Post: 07-14-2004, 04:20 PM

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