Archive

Archive for January, 2012

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

January 8, 2012 Leave a comment

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 010 is 0 in decimal. 112 is binary 11 and 1016 hexadecimal.

The binary system is the easiest one to start with. There are 2 possible values for each digit 0 and 1. 02 = 010 , 112 = 310 . 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 Base2. 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 (27) 64 (26) 32 (25) 16 (24) 8 (23) 4 (22) 2 (21) 1 (20)
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 010001012 = (1 * 64) + (4 * 1) + ( 1 * 1) = 6910. 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 2012610:

100000 (105) 10000 (104) 1000 (103) 100 (102) 10 (101) 1 (100)
0 2 0 1 2 6

Lastly, using the same pattern we can show 12710 in hexadecimal.

256  (162) 16 (161) 1 (160)
7 F

As per the table 7F = (7 * 16) + (15 * 1) = 12710 .

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.

 byte result = (17 | 5);
Console.WriteLine(result);
The output for the above operation will be 21. How do we apply a binary operator to byte values? You actually don’t, in the situation above the 17 is broken down to it’s binary equivalent and the 5 is broken down to it’s binary equivalent and each bit is “ORed”. Applying an OR operation to a binary  bit is easy to follow when you consider that a bit set to 0 is considered false and if a bit is set to 1 is considered true. With the OR operator if either side is set to true the result is always true. If both sides are false the result is always false. Below is a table that shows the example above’:
17 1 0 0 0 1
5 0 0 1 0 1
Result (21) 1 0 1 0 1
Once broken down to binary values, it demystifies the whole process of applying an OR operation to two bytes.
The AND operation when applied to bits only returns true (1) if both bits are true.
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.

To be Continued

Advertisements
Categories: Binary, C# Tags: