I have been programming for over an year now. I still don't get why bitwise OR and AND operators are used. They seem to return random numbers.
What mathematical purpose do they have?
Be sure to click 'more' and select 'suggest as answer'!
If you're the thread creator, be sure to click 'more' then 'Verify as Answer'!
I have been programming for over an year now. I still don't get why bitwise OR and AND operators are used. They seem to return random numbers.
What mathematical purpose do they have?
In low-level programming, you often use an integer to represent a collection of single bit fields, or fields that are only a few bits wide. The bit-wise operators are useful for checking the values of those fields or changing individual bits. A good example in GNU/Linux is the "file mode" field that represents permissions as a bit vector, often shown in octal. For example, "0664" = 110.110.100 means that the file is readable and writable by its owner and its group, but to the world it's read-only.
Data communications protocols often use individual bits and short fields in headers for various control purposes.
Bit-wise AND and OR are often used for device registers, where individual bits may represent the values of GPIO inputs or outputs.
Bit-wise XOR is often used for making linear-feedback shift registers, which may be used as pseudo-random number generators.
In low-level programming, you often use an integer to represent a collection of single bit fields, or fields that are only a few bits wide. The bit-wise operators are useful for checking the values of those fields or changing individual bits. A good example in GNU/Linux is the "file mode" field that represents permissions as a bit vector, often shown in octal. For example, "0664" = 110.110.100 means that the file is readable and writable by its owner and its group, but to the world it's read-only.
Data communications protocols often use individual bits and short fields in headers for various control purposes.
Bit-wise AND and OR are often used for device registers, where individual bits may represent the values of GPIO inputs or outputs.
Bit-wise XOR is often used for making linear-feedback shift registers, which may be used as pseudo-random number generators.