What is the purpose of the bitwise inclusive OR operator?

From what I understand, the bitwise inclusive OR operator compares every bit in the first and second operand and returns 1 if either bit is 1. Bjarne Stroustrup uses it like this (ist being an istream object):

ist.exceptions(ist.exceptions()|ios_base::bad_bit);

I haven't really worked with bits a lot in programming, should it be on my to-do list to learn? I understand that if I had an int and the value was 9, the binary would be 00001001, but that is pretty much it. I do not understand why he would use this operator in the context that he used it in.


You can think of it as a way to add an option to a set of existing options. An analogy would be if you're familiar with linux:

PATH = "$PATH:/somenewpath"

This says 'I want the existing path and this new path /somenewpath'

In this case he's saying 'I want the existing options in exceptions and I want the bad_bit option as well'


In this case, it simply means "turn a bit on".

Just an example: I have a byte 0100 0011 serves as 8 booleans. I want to turn on 4th bit (ie make 4th boolean true)

By bitwise operation, it looks like this: [0100 0011] Bitwise-OR [0000 1000] and it will give you 0100 1011 . Which means, it simply change 4th bit to true, regardless of its original value


The std::ios::exceptions is a function which gets/sets an exception mask in the file which is used by the file object to decide in which situations it should throw an exception or not.

There exist two implementations of this function:

  • iostate exceptions() const; // get current bit mask
  • void exceptions (iostate except); // set new bit mask
  • The statement you've posted sets the new exception mask to the file object using ios_base::badbit flag combined with the current flags, that are currently set in the file object.

    The OR bitwise operator is often used in order to create create a bitfield using already existing bitfield and a new flag. It could also be used in order to combine two flags together into a new bitfield.

    Here is an example with explanation:

    // Enums are usually used in order to represent 
    // the bitfields flags, but you can just use the
    // constant integer values.
    // std::ios::bad_bit is, actually, just a constant integer.
    enum Flags {
        A,
        B,
        C
    };
    
    // This function is similar to std::ios::exceptions
    // in the sense that it returns a bitfield (integer,
    // in which bits are manipulated directly).
    Something foo() {
      // Return a bitfield in which A and B flags
      // are "on".
      return A | B;
    }
    
    int main() {
      // The actual bitfield, which is represented as a 32-bit integer
      int bf = 0;      
    
      // This is what you've seen (well, somethng similar).
      // So, we're assigning a new bitfield to the variable bf.
      // The new bitfield consists of the flags which are enabled
      // in the bitfield which foo() returns and the C flag.
      bf = foo() | C;
    
      return 0;
    }
    
    链接地址: http://www.djcxy.com/p/75012.html

    上一篇: Capistrano V3在database.yml上失败

    下一篇: 按位包含OR运算符的目的是什么?