## Nibbles and Bits–A discussion of Binary Operations in C#

Before we start our discussion on Binary operations we should first discuss the different numerical systems. In general the most commonly used numbering systems (especially in programming) are Binary, Hexadecimal and Decimal.

Since all systems overlap some digits, in order to distinguish between the different numbering system we’ll use subscripts. For example 0_{10 }is 0 in decimal. 11_{2} is binary 11 and 10_{16} hexadecimal.

The binary system is the easiest one to start with. There are 2 possible values for each digit 0 and 1. 0_{2} = 0_{10} , 11_{2} = 3_{10 }. How does that work? It’s very simple. In the table below we have 8 positions, this is the same size as a byte. For each bit in the byte there is a binary value, meaning a 1 or 0. On the first row of the table it shows the value assigned to that bit. Because this system allow only two values (0,1) it’s called a Base_{2}. In the table below you’ll notice that it’s value is an exponent of 2. This is very important and the basis of all numbering systems.

128 (2^{7}) |
64 (2^{6}) |
32 (2^{5}) |
16 (2^{4}) |
8 (2^{3}) |
4 (2^{2}) |
2 (2^{1}) |
1 (2^{0}) |

0 | 1 | 0 | 0 | 0 | 1 | 0 | 1 |

In order to convert a binary number to decimal simply multiply the values in the top row with the values in the bottom row. So the value of 01000101_{2} = (1 * 64) + (4 * 1) + ( 1 * 1) = 69_{10}. Surprisingly this is the same method used for all numbering systems. For example, in the table below is the same representation in decimal for the number 20126_{10}:

100000 (10^{5}) |
10000 (10^{4}) |
1000 (10^{3}) |
100 (10^{2}) |
10 (10^{1}) |
1 (10^{0}) |

0 | 2 | 0 | 1 | 2 | 6 |

Lastly, using the same pattern we can show 127_{10} in hexadecimal.

256 (16^{2}) |
16 (16^{1}) |
1 (16^{0}) |

7 | F |

As per the table 7F = (7 * 16) + (15 * 1) = 127_{10} .

That’s the quick recap on the concept of numbering systems. Now on to bit wise operations.

## OR ( | ) and AND ( & )

OR operations in C# are accomplished by using the | operator. The logic behind the OR operations is frequently used in if statements so all programmers are familiar with it, however, when writing out a bitwise OR it appears confusing simply because it’s in decimal notation.

Console.WriteLine(result);

17 | 1 | 0 | 0 | 0 | 1 |

5 | 0 | 0 | 1 | 0 | 1 |

Result (21) | 1 | 0 | 1 | 0 | 1 |

17 | 1 | 0 | 0 | 0 | 1 |

5 | 0 | 0 | 1 | 0 | 1 |

Result (1) | 0 | 0 | 0 | 0 | 1 |

## Putting it all together

What is the point of using these operations? There are quite a few operations that require and OR operation, however, the most common use is for storing Boolean values in a byte or integer value. Let’s say you need to keep track of 5 settings, New, Archive, Done, Hidden, Valid. You could create 5 Boolean variables to track them or just store them as bits in a byte. There are a lot of advantage to it, mostly because you can combine two sets of result just by conducting an OR operation. As demonstrated above you can combine the settings from two bytes easily into one.