Common usage of the Bitwise OR (|)

I have been trying to find common uses for the Bitwise OR (|) and complement/NOT (~). I know how it works, I was just wondering how it's used and why in a program?

Thanks for any help.
they're typically used when you have a variable that can have one or more "flags" set. Each flag would be represented by a bit. You would use |, &, and ~ to manipulate certain bits.

Example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// flag states for opening files
//  note each value must be a single bit
enum
{
  Read = (1<<0),  // open with read permission
  Write = (1<<1), // write permission
  Binary = (1<<2), // open as binary
  Trunc = (1<<3) // truncate file contents
};

//....

// so if we want read + write permission + binary mode, we would
//  set all those flags with the | operator:
int flags = Read | Write | Binary;

// now if we want to remove Write permissions, we would clear that flag with
//  the & and ~ operators:
flags &= ~Write;
Ok, so you can set permissions to files with them. One question is the
Binary = (1<<2) so this sets everything to the binary value when you open the file? I'm going to try this out on my homework.

Thanks for your help & time.
Topic archived. No new replies allowed.