Negative Or

This is a discussion on Negative Or within the C++ Programming forums, part of the General Programming Boards category; I'm making a flags class in C++ for some practice. I have a function that should reset a flag. I ...

  1. #1
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218

    Negative Or

    I'm making a flags class in C++ for some practice. I have a function that should reset a flag. I figured it would work by NORing the bit value with the flags variable. I just cant work out how to stick not NOT in there. Heres my non-working code. It should explain better what I'm trying to do here:
    Code:
    bool Flag32::ResetBit(Uint8 bit)
    {
    	if(bit < 1 || bit > 32) return false;
    	flags !|= (1 << (bit-1));
    	return true;
    }
    Someone know how I can do this? I know I could do this by checking if the bit is on or off, then subtracting or not, I'd just like to be able to Nor stuff
    Last edited by mike_g; 09-12-2007 at 07:51 PM.

  2. #2
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,160
    AND it with the complement of the bit:

    Code:
    value &= ~flag;
    ~flag has 1's everywhere except at the actual flag, which is zero. So by ANDing this pattern with the value, you keep everything the same except the bit you want to turn off.

    EDIT: Also, this is a good opportunity to make this a template. Instead of Flag32, how about:

    Code:
    template <typename IntType>
    class Flag
    {
    };
    Now you can use any type you want as the underlying flags type.

  3. #3
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    Oh yeah I forgot that wavy thing is the binary inverter. Being able to set the field type would be better. I havent used any templates yet, but I guess this would be a good time to start. Cheers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Convert bytes to int, keep the negative number
    By umm in forum C Programming
    Replies: 7
    Last Post: 03-20-2009, 04:47 PM
  2. Keep getting negative sign after answer
    By uptown11 in forum C++ Programming
    Replies: 7
    Last Post: 02-23-2009, 10:14 PM
  3. Can we input negative numbers in the output windiw
    By hitesh1511 in forum C Programming
    Replies: 1
    Last Post: 08-22-2006, 12:07 AM
  4. Negative Numbers
    By Quantrizi in forum C++ Programming
    Replies: 7
    Last Post: 10-12-2003, 12:48 AM
  5. how to handle integer overflow in C
    By kate1234 in forum C Programming
    Replies: 8
    Last Post: 04-23-2003, 12:20 PM

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