# Thread: Using OR operator to combine two chunks of bits?

1. ## Using OR operator to combine two chunks of bits?

The exercise asks me to do:

"The left-shift operator can be used to pack two character values into an unsigned integer variable. Write a program that inputs two characters from the keyboard and passes them to function packCharacters. To pack two characters into an unsigned intger variable, assign the first character to the unsigned variable, shift the unsigned variable left by 8 bit positions and combine the unsigned variable with the second character using the bitwise inclusive OR operator. The program should output the character in their bit format before and after they are packed into the unsigned integer to prove that the characters are in fact pakced correctly in the unsigned variable."

How would I use an OR operator to combine two chunks of bits? My understanding is the OR operator will return the data-type filled with 1's if it evaluates true and all 0's if it evaluates false. It just doesn't make sense to me to want to do that. You can left shift the unsigned int, and then do a simple addition of the second character( after converting to int ). What am I missing?

2. No, bitwise or works as follows.
Code:
```  101
| 011
------
111```
The idea is that bitwise or would look at the individual bits and the result of the expression is each bit or'd together. Other than that, everything you know about or, including the truth table, applies.

The program on the other hand works because you would create space for the other char by shifting. You essentially or the char with a zero'd out portion of the unsigned value.

3. ahh ok.
I thought

Code:
```   001
|  100
----
111```
But you're saying the evaluation is done one bit at a time, and returns a value of 1 or 0 for that particular pair of bits.

Thanks!

4. Originally Posted by yougene
How would I use an OR operator to combine two chunks of bits? My understanding is the OR operator will return the data-type filled with 1's if it evaluates true and all 0's if it evaluates false. It just doesn't make sense to me to want to do that. You can left shift the unsigned int, and then do a simple addition of the second character( after converting to int ). What am I missing?
Nothing, really. The observation that you could add instead of OR is certainly true. In the case of adding disjoint sets of binary digits, the carry bit of the addition is always 0 and addition is equivalent to a binary OR. I think you're comprehending this stuff fine...

5. How would I use an OR operator to combine two chunks of bits? My understanding is the OR operator will return the data-type filled with 1's if it evaluates true and all 0's if it evaluates false. It just doesn't make sense to me to want to do that. You can left shift the unsigned int, and then do a simple addition of the second character( after converting to int ). What am I missing?
An important use of OR is
when you OR "something" with 0 you get back the "something"
You can make use of this here.

6. Crystal clear I think. Thanks guys.