bitwise operations

This is a discussion on bitwise operations within the C++ Programming forums, part of the General Programming Boards category; using bitwise operations how could i split a 16 digit binary number into two seperate 8 digit binary numbers?...

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    11

    bitwise operations

    using bitwise operations how could i split a 16 digit binary number into two seperate 8 digit binary numbers?

  2. #2
    Lean Mean Coding Machine KONI's Avatar
    Join Date
    Mar 2007
    Location
    Luxembourg, Europe
    Posts
    444
    like this:
    Code:
    int number_16bit = 0xFFFF;
    int number1_8bit = (number_16bit >> 8) & 0xFF;
    int number2_8bit = number_16bit & 0xFF;
    number2 will contain the least significant bits (right bits) and number1 will contain the leftmost bits. Since number1/number2 are only 8 bits, you can also use a char type to store the content.

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    11
    Quote Originally Posted by KONI
    like this:
    Code:
    int number_16bit = 0xFFFF;
    int number1_8bit = (number_16bit >> 8) & 0xFF;
    int number2_8bit = number_16bit & 0xFF;
    number2 will contain the least significant bits (right bits) and number1 will contain the leftmost bits. Since number1/number2 are only 8 bits, you can also use a char type to store the content.
    in that case will number2 the one that contains the least significant bits always contain 1111 1111?
    i obtained the left side of the 16 digit binary number by doing the following
    Code:
    	int value2 = FileSize >> 8;
    I am then going to conver the integer values into char values.

  4. #4
    Lean Mean Coding Machine KONI's Avatar
    Join Date
    Mar 2007
    Location
    Luxembourg, Europe
    Posts
    444
    Number2 will contain the least significant bits of your input since I masked it with an 0xFF mask. The bitwise AND together with the mask (0xFF) keeps the 8 least significant bits without changing their values and sets the rest to 0.

    Code:
    int value2 = FileSize >> 8;
    The code above only works if FileSize is exactly 16 bits, it won't work if FileSize is 17 bits or above. By masking with an 8 bit mask, you're making sure that the rest of the bits is discarded/set to zero.

  5. #5
    Registered User
    Join Date
    Oct 2005
    Posts
    11
    Quote Originally Posted by KONI
    Number2 will contain the least significant bits of your input since I masked it with an 0xFF mask. The bitwise AND together with the mask (0xFF) keeps the 8 least significant bits without changing their values and sets the rest to 0.

    Code:
    int value2 = FileSize >> 8;
    The code above only works if FileSize is exactly 16 bits, it won't work if FileSize is 17 bits or above. By masking with an 8 bit mask, you're making sure that the rest of the bits is discarded/set to zero.
    I know it will always be exactly 16 bits as i put the data in as 16 bits, thanks a lot for your help, anding a binary number with that mask only checks the first 8 digits and then set the rest to 0?
    so if i and the digits 1 0 1 1 1 1 1 1 0 1 0 1 1 1 1 0 with 0xFF the output should be 0 1 0 1 1 1 1 0.

  6. #6
    Lean Mean Coding Machine KONI's Avatar
    Join Date
    Mar 2007
    Location
    Luxembourg, Europe
    Posts
    444
    exactly, this is because the mask 0xFF has every bit set to 0 if you bitwise AND it with a number larger than 8 bits. And because, if you AND whatever bit with 0, the result is 0, you eliminate all the bits apart from the 8 first ones.

  7. #7
    Registered User
    Join Date
    Oct 2005
    Posts
    11
    But for joining them back together as one int would i have too loop around each individual bit, for both of the seperate numbers, comparing them against a 16 bit 0 number?

  8. #8
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    Or you could just shift them in together in two swppos. Kind of the opposite of what you did to extract them in the first place.

  9. #9
    Lean Mean Coding Machine KONI's Avatar
    Join Date
    Mar 2007
    Location
    Luxembourg, Europe
    Posts
    444
    Quote Originally Posted by black_watch
    But for joining them back together as one int would i have too loop around each individual bit, for both of the seperate numbers, comparing them against a 16 bit 0 number?
    Like this:
    Code:
    number_16bit = (number1_8bit << 8) | number2_8bit;
    C makes sure that, if you left shift a number, only 0's will be added.

  10. #10
    Registered User
    Join Date
    Oct 2005
    Posts
    11
    That worked great, thanks a lot guys, i think im understanding this whole bitwise thing a little better now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bitwise Operations to Read ON/OFF Bits
    By pseudonoma in forum C Programming
    Replies: 4
    Last Post: 02-25-2008, 03:15 PM
  2. bitwise operations with double
    By henry_kay in forum C Programming
    Replies: 2
    Last Post: 10-03-2007, 05:57 AM
  3. Bitwise operations
    By sh3rpa in forum C++ Programming
    Replies: 16
    Last Post: 09-25-2007, 07:32 PM
  4. bitwise operations
    By andrew_tucker in forum C Programming
    Replies: 2
    Last Post: 11-28-2002, 12:46 AM
  5. bitwise operations
    By bukko in forum C Programming
    Replies: 3
    Last Post: 10-06-2001, 07:56 AM

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