**By Mark Schweikert**

If you haven't heard already, a byte is a sequence of eight binary units. These binary units are called bits. While this is no great secret, the functioning of bits can be confusing when just observing their combined value at the byte level. In this article we shall endeavor to uncover the inner workings of bits and hopefully discover their simplicity.

A Bit is the most basic form of memory in a computer. It can have only one of two values, On or Off. There are a few ways we will to refer to these two values: On/Off, One/Zero, True/False, and Set/Clear. We will visit each way of looking at bit values as the article progresses. Let us start by examining bit uniqueness.

**Bit Uniqueness**

When I first learned about bits and bytes I asked myself, “How can you tell which bit is which? And how can a byte hold a value from 0 to 255 if it only has 8 bits?” A byte cannot be just a jumble of bits in a sack, there needs to be order. The answer is binary numbers! A binary is anything which is comprised of two components. In computers a binary digit will have the components of 1 and 0. If you sequence eight binary digits together you get a byte. There are 256 possible combinations of eight binary digits, so a byte can hold 256 different values. When a binary number is represented on paper each additional bit is added on to the left making the bit orientation 76543210. This is just how units are added to natural numbers so this part should be familiar to everyone. Instead of having ones, tens, hundreds, etc. we have bit 0, bit 1, bit 2, etc.

A byte with a value of zero (with all bits turned off) would look like this 00000000 in binary.

A byte with a value of 255 (with all bits turned on) would look like this 11111111 in binary.

OK that's easy enough, but how do we determine the values between the two extremes? How do we know that 00100000 = 32? There is a very simple system for converting binary numbers into something the average human can understand. Since each bit can have two values we can determine each bit value by using 2n where n is the bit number.

- bit 0 = 20 = 1
- bit 1 = 21 = 2
- bit 2 = 22 = 4
- bit 3 = 23 = 8
- bit 4 = 24 = 16
- bit 5 = 25 = 32
- bit 6 = 26 = 64
- bit 7 = 27 = 128

Because the bit values double with each bit no single bit can have a value equal to any combination of the other bit values; this is efficiency at it's best. Try it, add any combination of bits together, it will be a unique value. Let us revisit the binary number 00100000. If we put it in a bit list like above it's value is easily verified as 32:

- bit 0 is off = 0
- bit 1 is off = 0
- bit 2 is off = 0
- bit 3 is off = 0
- bit 4 is off = 0
- bit 5 is on = 32
- bit 6 is off = 0
- bit 7 is off = 0

Add up all the values and we get 32. Lets try a harder one, how about 01100111.

- bit 0 is on = 1
- bit 1 is on = 2
- bit 2 is on = 4
- bit 3 is off = 0
- bit 4 is off = 0
- bit 5 is on = 32
- bit 6 is off = 64
- bit 7 is off = 0

Add up all the values and we get 103, so 01100111 = 103.

Since each bit has a unique value this allows us to determine which bits are on and which bits are off. Of course it would be silly to try to determine the bit pattern by trial and error, we will use a more reliable method, boolean logic.

**Boolean Logic**

Boolean logic is a formal system for comparing two values. It provides us with all the tools we need for examining byte values. No computer can function without being able to examine memory down to the bit level. For this reason all programming languages contain boolean functions. The functions we are going to focus on are AND, OR (inclusive OR) and XOR (exclusive OR). With these three functions we can test, set, and clear bits within a byte. There are two ways your can use boolean functions; if you assign the boolean function to a byte variable you will get the byte value; if you assign the boolean function to a boolean variable you will get a true or false value. We will see this demonstrated in a moment. Below are the truth tables for our three boolean functions and examples of each. Do not worry if the tables are confusing, they will slowly become clear as we work through the details of each boolean function.

**Boolean Logic - Truth Tables**

Let us declare two variables to store the values returned from our boolean functions using pseudocode. One will be declared as a byte data type, the other will be declared as a boolean data type. We can get valuable information from returning values to both data types. We will use the values from the examples above to aid in understanding.

Declare myByte as Byte

Declare myBoolean as Boolean

**OR: returning a boolean value**

The OR function will return a True value as long as at least one byte being compared has a bit turned on. In other words, the only way for the OR function to return a False value would be for both bytes to have a value of zero.

**myBoolean** **= 59 OR 82** will return a value of True because one of the values being compared has at least one bit turned on. In this case they both have at least one bit turned on.

**OR: returning a byte value**

The OR function will return the sum of the values of all bits that are turned on in either byte. Having the same bit turned on in both bytes has no bearing on the result. This is known as an Inclusive OR , the bit value is returned if one, or the other, or both have the bit turned on; in truth table terms an Inclusive OR is True if one, or the other, or both are True.

**XOR: returning a boolean value**

The XOR function will return a True value only if a bit is turned on in only one of the two bytes being compared. In other words, the only way for the XOR function to return a False value would be for both bytes to have the same value.

**myBoolean** **= 59 XOR 82** will return a value of True because one of the bytes being compared has at least one bit different from the other byte. In this case they both have at least one bit turned on that the other byte has turned off.

**XOR: returning a byte value**

The XOR function will return the sum of the values of all bits that are turned on in one byte but not the other. Having the same bit turned on in both bytes will make the function exclude that bit value. This is known as an Exclusive OR , the bit value is returned if only one of the two bytes has the bit turned on. In general terms, you can have one or the other but not both; in truth table terms an Exclusive OR is True if one of the two values being compared is True.

**myByte = 59 XOR 82** will return a value of 105 because bits 0,3, and 5 are only turned on in the first Byte and bit 6 is only turned on in the second byte; 1+8+32+64=105

**AND: returning a boolean value**

The AND function will return a True value only if both bytes being compared have at least one bit in common.

**myBoolean = 59 AND 82** will return a value of True because (as can be seen in the examples) both bytes have at least one bit in common. In this case they have two bits in common, bit 1 and bit 4.

**AND: returning a byte value**

The AND function will return the sum of all common bits between the two bytes being compared.

**myByte = 59 AND 82** will return a value of 18 because both bytes have bit 1 (value of 2) and bit 4 (value of 16) in common; 2+16=18.

**Putting All of This to Practical Use**

- Declare theByte as Byte
- Declare theBitValue as Byte
- Declare theBit as Integer

To test a bit in a byte we need a boolean function that will only be true if both values are true; we need the AND function.

- theByte = 160 '(bits 5 and 7 are turned on)
- theBit = 5 (we want to test bit 5)
- theBitValue = 2theBit (calculate the bit value of the test bit)
- myBoolean = theByte AND theBitValue
- myByte = theByte AND theBitValue

The variable myBoolean will return a value of True because bit 5 is in fact turned on in theByte. If it had been turned off, say only bit 7 was turned on with a value of 128 then myBoolean would have returned a False value.

The variable myByte will return a value of 32 because bit 5 is in fact turned on in theByte and the value of bit 5 is 32. If it had been turned off the returned value would have been zero.

**Setting a bit**

- theByte = 128 (bit 7 is turned on)
- theBit = 5 (we want to set bit 5)
- theBitValue = 2theBit (calculate the bit value of the test bit)
- myByte = theByte OR theBitValue

The variable myByte will return a value of 160 because theByte has a value of 128 and theBitValue is 32. The OR function is merging the byte with the bit. If bit 5 had already be turned on in theByte (giving it a value of 160) the returned value would be 160. This is because we have just told the computer to set bit 5, it doesn't care that bit 5 is already set. It sets it again, no harm done.

**Clearing a bit**

To clear a bit in a byte is a little tricky, not difficult, just tricky. We will use the AND function. Since AND only returns a True if both values are True we can use it along with a byte mask to get the desired result. The byte mask is essentially a negative image of our bit. If we think of the bit as a byte that only has that one bit turned on, then the byte mask has all BUT that one bit turned on. Since this is substantially different that what we have done up to this point I have provided both a new variable and a table which will help make it clearer.

- Declare theByteMask as Byte
- theByte = 160 (bits 5 and 7 are turned on)
- theBit = 5 (we want to clear bit 5)
- theByteMask = 255 - theBit (create the byte mask)
- myByte = theByte AND theByteMask
- Byte Masking

As you can see, because bit 7 is the only bit in common it is the only value returned. That is the key to the byte mask. The byte mask will always have something in common with every bit except for the one you want to clear. This is my favorite bit manipulation because we are manipulating both the bits and the AND function!

**Flipping a bit**

Some times you will need to simply flip the bit from on to off or vise versa. Using the methods we have discussed up to this point we would have to first test the byte to determine if the bit is on or off; then we would have a conditional to decide whether to use the set method or clear method. That is just too much work, especially when an Exclusive OR can be used to just flip the bit regardless of it's value. Remember the exclusive or is True only if one (and only one) out of the two values being compared is True. If the bit is already turned on in the byte when we XOR the same value both will be True so the result will be zero and will clear the bit. If the bit is not turned on in the byte when we XOR the bit value (one is True and one is False) the result will be False and the bit will be set . To demonstrate bit flipping we will get a little fancy and flip two bits at the same time.

- theByte = 128 (bit 7 are turned on)
- theBitValue = 25 + 27 (calculate the bit values and add them together)
- myByte = theByte XOR theBitValue

The variable myByte will return a value of 32. Bit 7 was in both values so XOR returned a zero for that bit. Bit 5 was only in theBitValue so XOR returned the value of the bit or 32. 0+32=32.

**Summary**

Logic... All computers are based on logic. The fundamental building block of a computer is the bit. To interact with bits we need to use? Logic of course. Boolean logic and bits are both very simplistic, while some of the topics we have covered may seem confusing, the more you use them the more they make sense. Soon, if not already, you will find bit manipulation to be so easy that you don't understand why you haven't been doing it all along. There are some pretty cool things that can be done using bit manipulation. In future articles and/or tutorials we will explore the possiblities. Until then, I hope I have taught you how to be manipulative.

**Author Information:**

Mark Schweikert

http://www.programmers-corner.com

modusponens@toadmail.com

**Comments:**

Add your comments here.

Name

Comment

You can also send feedback to feedback@programmers-corner.com

**Pockyzung -** May 15, 2005 8:53 AM

So, I'm never think about that. It's manking me understand about Declaration Var. Thank you, Very much all programmer.