## Definition

“Of all positional systems, the binary number system seems to be the simplest. 2 is the radix or the base of the system, meaning that only two digits—represented by 0 and 1—appear in the system. Today, this number system is used in every digital computer. The two digits, 1 and 0, are considered as the two states (off/on) and these states are used to carry instructions and store data in computers. Generally, this element represents just one bit which is referred to as binary digit.”^{1}

## Who

If you are going to be in any field that uses mathematics (e.g., an applied mathematician, a statistician, or a data scientist), you are probably going to do programming on some type of computer, e.g., Raspberry Pi, PC or mainframe computer. You should know how to do binary math and bitwise operations.

“I have noticed that some people seem to have problems with bitwise operators, so I decided to write this brief tutorial on how to use them.”^{2}

## What

Bits, what are they, you may ask? Well, simply put, bits are the individual ones and zeros that make up everything we do with computers. All the data you use is stored in your computer using bits. A BYTE is made up of eight bits, a WORD is two BYTEs, or sixteen bits. And a DWORD is two WORDS, or thirty two bits.

```
0 1 0 0 0 1 1 1 1 0 0 0 0 1 1 1 0 1 1 1 0 1 0 0 0 1 1 1 1 0 0 0
|| | | | ||
|+- bit 31 | | | bit 0 -+|
| | | | |
+-- BYTE 3 -----+--- BYTE 2 ----+--- BYTE 1 ----+-- BYTE 0 -----+
| | |
+----------- WORD 1 ------------+----------- WORD 0 ------------+
| |
+--------------------------- DWORD -----------------------------+
```

The beauty of having bitwise operators is that you can use a BYTE, WORD or DWORD as a small array or structure. Using bitwise operators, you can check or set the values of individual bits or even a group of bits.^{2}

In C, the following 6 operators are bitwise operators (work at bit-level) ^{5}

- The
**& (bitwise AND)**in C or C++ takes two numbers as operands and does AND on every bit of two numbers. The result of AND is 1 only if both bits are 1.

- The
**| (bitwise OR)**in C or C++ takes two numbers as operands and does OR on every bit of two numbers. The result of OR is 1 if any of the two bits is 1.

- The
**^ (bitwise XOR)**in C or C++ takes two numbers as operands and does XOR on every bit of two numbers. The result of XOR is 1 if the two bits are different.

- The
**<< (left shift)**in C or C++ takes two numbers, left shifts the bits of the first operand, the second operand decides the number of places to shift.

- The
**>> (right shift)**in C or C++ takes two numbers, right shifts the bits of the first operand, the second operand decides the number of places to shift.

- The
**~ (bitwise NOT)**in C or C++ takes one number and inverts all bits of it

## Why

” I think bitwise masks are a great tool that should be in any devs belt.”^{3}

“My main use for bitwise operators could be relevant anywhere – representing a set of flags. For instance, you might have an integer in the database representing a set of security permissions for a user, and in your web app you would have to check those before continuing.”^{3}

“When working on limited memory devices, you can’t really afford to have a thousand Boolean flags assigned to variables. This is a really inefficient way to store your flags because Boolean values in C are basically a byte long. We don’t need an entire byte to store just a zero or a one. The rest of the bits are going to remain zero throughout. You cannot afford to throw away precious memory when you’re working with just KBs of RAM.”^{4}

See Theoretical Knowledge Vs Practical Application.

## How

Many of the **References** and **Additional Reading** links, and **Videos** will assist you with understanding and using binary numbers.

As some professors say: “It is intuitively obvious to even the most casual observer.”

## References

^{1} “The Binary Number System: Its History, Applications And Advantages | Binary Convertor”. 2021. *binarytranslator.com*. http://www.binarytranslator.com/the-binary-number-system-its-history-applications-and-advantages.

^{2} Arends, PJ. 2002. “An Introduction To Bitwise Operators”. *codeproject.com*. https://www.codeproject.com/Articles/2247/An-introduction-to-bitwise-operators.

^{3} Aron Rotteveel, and Jon Skeet. 2008. “When To Use Bitwise Operators During Webdevelopment?”. *Stack Overflow*. https://stackoverflow.com/questions/261062/when-to-use-bitwise-operators-during-webdevelopment.

^{4} “Real World Uses Of Bitwise Operators”. 2020. *Medium*. https://blog.tarkalabs.com/real-world-uses-of-bitwise-operators-c41429df507f#42c6.

^{5} “Bitwise Operators In C/C++ – GeeksForGeeks”. 2014. *GeeksForGeeks*. https://www.geeksforgeeks.org/bitwise-operators-in-c-cpp/.

## Additional Reading

“Binary Division – Rules, Steps To Solve, Examples”. 2021. *Cuemath*. https://www.cuemath.com/numbers/binary-division/.

“Binary Number – Wikipedia”. 2021. *en.wikipedia.org*. https://en.wikipedia.org/wiki/Binary_number.

“Binary Numbers | Teachallaboutit”. 2018. * TeachAllAboutIt | Computer Science Tuition, Resources, & Live Lessons*. https://teachallaboutit.school/gcse-computer-science/ocr-gcse-j276/number-systems-binary/.

“Binary Number System”. 2021. *mathsisfun.com*. https://www.mathsisfun.com/binary-number-system.html.

“Binary Multiplication – Rules, Method To Multiply Binary Numbers, Examples.”. 2021. *Cuemath*. https://www.cuemath.com/numbers/binary-multiplication/.

“Binary Number System – Definition, Conversion And Examples”. 2021. *BYJUS*. https://byjus.com/maths/binary-number-system/.

“Bits, And Bitwise Operators”. 2021. *cs.uaf.edu*. https://www.cs.uaf.edu/courses/cs301/2014-fall/notes/bits-bitwise/.

“Bitwise Operators In C/C++ – GeeksForGeeks”. 2014. *GeeksForGeeks*. https://www.geeksforgeeks.org/bitwise-operators-in-c-cpp/.

“GCSE Computer Science | Binary Addition | TeachAllAboutIt”. 2018. *TeachAllAboutIt | Computer Science Tuition, Resources, & Live Lessons*. https://teachallaboutit.school/gcse-computer-science/binary-addition/.

“GCSE Computer Science | Binary Multiplication | TeachAllAboutIt”. 2018. *TeachAllAboutIt | Computer Science Tuition, Resources, & Live Lessons*. https://teachallaboutit.school/gcse-computer-science/binary-multiplication/.

“How To Use Bitwise Operators”. 2020. *Medium*. https://betterprogramming.pub/how-to-use-bitwise-operators-90cd7a3a0fd7.

“Introduction To Number Systems And Binary (Video) | Khan Academy”. 2021. *Khan Academy*. https://www.khanacademy.org/math/algebra-home/alg-intro-to-algebra/algebra-alternate-number-bases/v/number-systems-introduction.

“The Binary Number System: Its History, Applications And Advantages | Binary Convertor”. 2021. *binarytranslator.com*. http://www.binarytranslator.com/the-binary-number-system-its-history-applications-and-advantages.

“Why Do Computers Use Binary Numbers| Convert Into Binary Online”. 2021. *binarytranslator.com*. http://www.binarytranslator.com/why-binary-numbers-are-used-by-computers.