difference between | and ||

|| is used for logic operations normally used in if statement but what is | is it same as | if we write if (3||4) and than if (3|4) are both statements same another code is that i find confusing is :

1
2
file.open(“GROUP.DAT”, ios::app | ios::out |
		ios::in | ios::binary );
I know that you often connect flags like ios::app or WS_OVERLAPPEDWINDOW wiv | and not wiv ||. But i think it makes no difference. Once i used || instead of | and it worked too.
But i dont know if you can do

if(p==3 | p==4)
...
The operator || operates on boolean values, that is simple true or false values.

The | operator is used to work with the individual bits of a binary numbers.

Example 3 | 4

in binary
3 = 0011
4 = 0100
result is 0111 (binary) which is 7 decimal.

Please see the tutorial here:
http://www.cprogramming.com/tutorial/bitwise_operators.html

Also for conversion between decimal, binary and hexadecimal, the online conversion here can be useful:
http://www.mathsisfun.com/binary-decimal-hexadecimal-converter.html


This statement:
file.open(“GROUP.DAT”, ios::app | ios::out | ios::in | ios::binary );
is using the bitwise or operator to combine a number of flags. Think of each value such as ios::out or ios::app as being a binary number with a single bit set, perhaps something like this: 00010000 or 00000010 (I don't know the actual values).
Last edited on
Thank you :)
Topic archived. No new replies allowed.