Using OR operator to combine two chunks of bits?

This is a discussion on Using OR operator to combine two chunks of bits? within the C Programming forums, part of the General Programming Boards category; The exercise asks me to do: "The left-shift operator can be used to pack two character values into an unsigned ...

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    182

    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. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,707
    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. #3
    Registered User
    Join Date
    May 2006
    Posts
    182
    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. #4
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    Quote Originally Posted by yougene View Post
    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...
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  5. #5
    and the hat of copycat stevesmithx's Avatar
    Join Date
    Sep 2007
    Posts
    498
    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.
    Not everything that can be counted counts, and not everything that counts can be counted
    - Albert Einstein.


    No programming language is perfect. There is not even a single best language; there are only languages well suited or perhaps poorly suited for particular purposes.
    - Herbert Mayer

  6. #6
    Registered User
    Join Date
    May 2006
    Posts
    182
    Crystal clear I think. Thanks guys.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Working with bits and SHA-1
    By Desolation in forum C++ Programming
    Replies: 6
    Last Post: 12-28-2008, 12:34 AM
  2. SDLKey to ASCII without unicode support?
    By zacs7 in forum Game Programming
    Replies: 6
    Last Post: 10-07-2007, 03:03 AM
  3. Help counting number of bits set in an integer
    By JayDiddums10 in forum C Programming
    Replies: 5
    Last Post: 12-07-2006, 02:21 PM
  4. Writing binary data to a file (bits).
    By OOPboredom in forum C Programming
    Replies: 2
    Last Post: 04-05-2004, 03:53 PM
  5. New idea on conveting byte to bits/bits to byte
    By megablue in forum C Programming
    Replies: 10
    Last Post: 10-26-2003, 01:16 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21