+ 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?

10th Sep 2020, 2:42 PM
StealthyTaco
StealthyTaco - avatar
5 Réponses
+ 10
some bitwise examples you can check it here: • Bitwise tricks — https://gist.github.com/2365607
10th Sep 2020, 8:23 PM
Danijel Ivanović
Danijel Ivanović - avatar
+ 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
10th Sep 2020, 2:52 PM
Arsenic
Arsenic - avatar
+ 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.
10th Sep 2020, 3:47 PM
arturop200
arturop200 - avatar
+ 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
10th Sep 2020, 4:20 PM
Steven M
Steven M - avatar
+ 3
Thanks everyone, several great answers!
10th Sep 2020, 10:11 PM
StealthyTaco
StealthyTaco - avatar