+ 2
Use cases for bitwise operations
Can anyone provide some examples of why we would use bit wise operations, ive already read about the power of 2, but other than that why might it be useful to compare the bits that make up a number?
6 Answers
+ 10
some bitwise examples you can check it here:
âą Bitwise tricks â https://gist.github.com/2365607
+ 8
You can check wether a number is even or not by using bitwise & operator.
Like this đ
If(num&1)
{
cout<<"it is odd";
}
It is I think the fastest way to check for parity of a number.
There are also a lot more uses of bitwise operations, you should have a look at bitmasking to get an idea
+ 6
In microcontrollers the Input/Output we can read the port register and use bitwise operations to change value of selected bits, then we write the result to the port register with the new configuration or whatever.
microcontrollers and external devices have registers in which one bit in a byte register works as flag or interruptor of a feature configuration.
+ 4
To Arsenic 's point... Bitwise operations are also used for error/fault detection. These parity bits will turn LEDs on, when a fault is present. This design is used in vehicle dashboards and other analog/digital visual indication layouts that require a visual and/or audio alert of a fault or error (Aircraft, Radars, etc.). If your check oil light is on, it is probably due to a failed parity check. Sure, there are other reasons why the light is on, but the "faults" associated with the faults would fail the parity check. The expectation is a 0, but it got a 1. That 1 bit will provide roughly 5vdc to light the "check oil" LED. Look at it like a fault isolation test.
https://www.tutorialspoint.com/what-is-a-parity-bit
+ 3
Thanks everyone, several great answers!