Why use OR in setf(...) ?

This is a discussion on Why use OR in setf(...) ? within the C++ Programming forums, part of the General Programming Boards category; When setting 2 or more stream flags together , why is the '|' operator used instead of &? Code: cout.setf(ios::showpos ...

  1. #1
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498

    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 ?
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  2. #2
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    681
    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).
    I never put signature, but I decided to make an exception.

  3. #3
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    *Thanks*
    I totally overlooked the low level..
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



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

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