bitset AND operator question

Copied this from the reference page
bitset<4> first (string("1001"));
bitset<4> second (string("0011"));

cout << (first^=second) << endl; // 1010 (XOR,assign)
cout << (first&=second) << endl; // 0010 (AND,assign)
cout << (first|=second) << endl; // 0011 (OR,assign)

1001 bitwise AND 0011 is 0001, isn't it?
also how come 1001 bitwise OR 0011 turn out to be 0011?
your are doing:

1001 ^ 0011 = 1010
1010 & 0011 = 0010
0010 | 0011 = 0011


Note that you are doing operation= ... first is being continually changed.
also how come 1001 bitwise OR 0011 turn out to be 0011?


It's not. 1001 OR 0011 is 1011

Look at the code. Notice that you're using the assignment operators (^=, &=, |=). So each time you're modifying first.

1
2
3
4
5
6
7
8
cout << (first^=second) << endl; // 1001 XOR 0011 = 1010
// first now == 1010

cout << (first&=second) << endl; // 1010 AND 0011 = 0010
// first now == 0010

cout << (first|=second) << endl; // 0010 OR 0011 = 0011
// first now == 0011 


EDIT: doh, too slow
Last edited on
Topic archived. No new replies allowed.