# How to Set a Bit

### Introduction

So far, we've written functions to determine the value of individual bits of a number, or to determine if, within some range of bits, whether any bit was set.

I'm sure you're interested in actually modifying bits. Once you understand masks, it's easy to modify individual bits.

Here's the prototype of the function we wish to implement.

```unsigned char setBit( unsigned char ch, int i ) ;
```

This will take a character as input, set bi to 1, and return that new character, while leaving the original character untouched (which happens because the original character is passed by value, which means it's a copy of the argument).

Here's the code:

```unsigned char setBit( unsigned char ch, int i )
{
unsigned mask = 1 << i ;  // we could cast to unsigned char, just to be safe
return mask | char ;  // using bitwise OR
}
```

Let's see how this works on an example:

 b7 b6 b5 b4 b3 b2 b1 b0 ch 1 0 0 0 0 0 1 1 mask 0 0 0 0 1 0 0 0 ch & mask 1 0 0 0 1 0 1 1

Instead of bitwise AND, we use bitwise OR. If you perform bitwise OR on a bit b with 0, you get back bit b. Thus, for all the bit positions in the mask with zero (in the example above, it's every bit except bit b3 in the mask), the corresponding bit position of ch & mask is just the value of the corresponding bit position in ch.

In other words, whereever there's a 0 in the mask, the original bit value in ch is preserved. For example, b7 = 1 in ch and the mask has a 0 in that position. The result ch & mask also has a 1 at that same bit position.

In fact, the only bit position that can potentially change in the result is the bit position that's a 1 in the mask. The mask has a 1 at b3. When you OR a bit b with 1, you get a 1 as a result.

Thus, this causes b3 to be 1 regardless of what b3 is in ch.

This is exactly the effect we're looking for. Setting b3 to 1 in the result, but leaving all other bits unchanged.

### Code You Can Write

Now that you know how to check if a bit is set, or if any bit is set in a range of bits, or to set a bit to 1, here are some other functions you may choose to write.

• Write a function that clears bit i (i.e., makes bit i's value 0).
• Write a function that sets bit from bhigh...blow to all 1's, while leaving the remaining bits unchanged.
• Write a function that clears bits from bhigh...blow to all 0's, while leaving the remaining bits unchanged.
This should be straight-forward if you understand the ideas behind the last few sets of notes.