Thread: Why use OR in setf(...) ?

  1. #1
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    San Jose
    Posts
    2,633

    Why use OR in setf(...) ?

    When setting 2 or more stream flags together , why is the '|' operator used instead of &?
    Code:
    cout.setf(ios::showpos | ios::showpoint);
    Wouldn't a & make more sense ?
    Is there a logical explanation for it ?

  2. #2
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    733
    It is used to set certain bits. If you have two flags:
    00100000
    00010000
    and you want to merge them together into:
    00110000
    you use bitwise or. The result of & would be 00000000.

    You could also use + instead, but if the same flag appears 2 times the result will be different than expected (some unwanted flag will be set).

  3. #3
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    San Jose
    Posts
    2,633
    *Thanks*
    I totally overlooked the low level..

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. declaration to use "setf"
    By Claire in forum C++ Programming
    Replies: 2
    Last Post: 01-18-2002, 06:04 PM