Thread: Bitwise set and mask issue

  1. #1
    Registered User hamsteroid's Avatar
    Join Date
    Mar 2007
    Location
    Waterford, Ireland
    Posts
    62

    Bitwise set and mask issue

    Hi,

    I have this piece of code, just testing the set and mask functions. Here I have a original hex string ABC and I want to put it into result in reverse. I have the mask as 0xF (1111) and take each letter at a time and set it in result, then I left shift the result var bits 4 places to make room for the next letter and right shift the original var 4 bits to get at the next letter (4 bits). However running this code does not turn ABC into CBA but rather F. Going through the code again and again - I can't see what's wrong with it. Can you? The vars are unsigned ints (no 2 complement here).

    Code:
    int main(void)
    {
      unsigned int original = 0xABC;
      unsigned int result = 0;
      unsigned int mask = 0xF;          /* rightmost 4 bits 1111 F is 16 over 4 bits */
      
      printf("\nOriginal = %X", original);
    
      /* Insert first digit in result */
      result |= original&mask;         /* put right 4 bits from original in result */
      
      /* Get second digit */
      original >>= 4;                    /* shift original right 4 places */
      result << 4;                      /* make room by shifting left 4 places */
      result |= original&mask;        /* put right 4 bits from original in result */
    
      /* Get third digit */
      original >>= 4;
      result << 4;
      result |= original&mask;        /* reminder - | sets and & tests */
      
      printf("\t result = %X\n", result); 
      return 0;
    }
    Last edited by hamsteroid; 04-02-2007 at 01:01 PM. Reason: more concise update

  2. #2
    Lean Mean Coding Machine KONI's Avatar
    Join Date
    Mar 2007
    Location
    Luxembourg, Europe
    Posts
    444
    You do it right 2 times and still wrong 2 other times:
    Code:
      result <<= 4;

  3. #3
    Registered User hamsteroid's Avatar
    Join Date
    Mar 2007
    Location
    Waterford, Ireland
    Posts
    62
    Quote Originally Posted by KONI View Post
    You do it right 2 times and still wrong 2 other times:
    Code:
      result <<= 4;
    Thanks!

    <<= <--- I'm going to make a computer game character outta that guy so I don't forget to check that again in a hurry next time. Thanks Koni. I was staring at that for 10 minutes at the word by word level.
    Last edited by hamsteroid; 04-02-2007 at 01:18 PM. Reason: typos? me?!

  4. #4
    Registered User hamsteroid's Avatar
    Join Date
    Mar 2007
    Location
    Waterford, Ireland
    Posts
    62
    Actually, my head gets a bit fuzzy when describing multiple hex characters in 1 unsigned int var. ie, mask 0xF looks like = 1111. I understand 0xF as it takes the maximum value 16 in binary.

    But say for 0xABC... Would it look like this? ie, 3 characters appended. ie

    A=10
    B=11
    C=12

    = 10 || 11 || 12
    = 1010 || 1011 || 1100
    = .......101010111100

    Would be ...........00000101010111100 be the final representation for 0xABC in a unsigned int?

    so.... after reversing look like.....00000110010111010 for 0xCBA. bitwise shifting is pretty cool. I bet you could have fun with that!
    Last edited by hamsteroid; 04-02-2007 at 01:59 PM. Reason: more info

  5. #5
    Lean Mean Coding Machine KONI's Avatar
    Join Date
    Mar 2007
    Location
    Luxembourg, Europe
    Posts
    444
    apart from the fact that A is actually 10, that's correct.

  6. #6
    Registered User hamsteroid's Avatar
    Join Date
    Mar 2007
    Location
    Waterford, Ireland
    Posts
    62
    Thanks corrected...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bitwise Operators
    By rrc55 in forum C Programming
    Replies: 6
    Last Post: 04-30-2009, 11:37 AM
  2. Issue w/ Guess My Number Program
    By mkylman in forum C++ Programming
    Replies: 5
    Last Post: 08-23-2007, 01:31 AM
  3. Why only 32x32? (OpenGL) [Please help]
    By Queatrix in forum Game Programming
    Replies: 2
    Last Post: 01-23-2006, 02:39 PM
  4. Editor problems
    By VirtualAce in forum Game Programming
    Replies: 2
    Last Post: 10-24-2005, 10:54 PM
  5. Number of set bits
    By Roaring_Tiger in forum C Programming
    Replies: 8
    Last Post: 09-22-2004, 09:05 PM