Thread: 3 each 8bit chars from a 32bit long int

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    5

    Question 3 each 8bit chars from a 32bit long int

    If I have a 32 bit address that I can incroment with memadd++

    long int memadd;
    char memBiteLow;
    char memBitemed;
    char memBiteHigh;

    memadd = 0x00ABCDEF;

    and I need

    I need

    MemBiteLow to hold EF
    MemBiteMed to hole CD
    MemBiteHigh to hold AB
    do I use union? struct?

    I do not seem to be able to make union work. All three chars have EF.

  2. #2
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Point to memadd using a char*, walk along it and store the dereferenced value in the high(low), med and low(high) variables, depending on the endianness of the machine.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Look into the bit shift (>>) and bit-wise AND (&) operators. Something like:
    Code:
    unsigned long memadd = 0x00ABCDEF;
    unsigned char memByteLow;
    unsigned char memByteMed;
    unsigned char memByteHigh;
    
    memByteMed = (unsigned char) ((memadd & 0x0000FF00) >> 8);
    Note that you always want to use unsigned values when using the bit shift operators. Otherwise, you will run into problems if the left-most bit is a 1 (it will be undefined or implementation defined behavior). Also, for future reference, there are left-shift operator (<<), bit-wise OR (|), bit-wise XOR (^) and bitwise NOT (~) operators. You should read your books and check some tutorials on how to use them. We have one here, and Google should turn up several more.

  4. #4
    Registered User
    Join Date
    Aug 2011
    Posts
    5
    Thanks. That is a brute force solution. I was looking for something more elegant.

  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Hmm...I wouldn't consider itCbitC's solution or mine to be "brute force". Actually, my solution is the only portable one I'm aware of (it works regardless of endianness of the machine). I would argue that code that doesn't need to check endianness before extracting bits is a lot more elegant than code that does. Here's a solution using a union that I really don't recommend, but might fit your definition of "elegant".

    Code:
    union {
        unsigned long memadd;
        unsigned char memBytes[sizeof(unsigned long)];
    } mem;
    
    if little endian
        low = mem.memBytes[0];
        med = mem.memBytes[1];
        high = mem.memBytes[2];
    else
        low = mem.memBytes[3];
        med = mem.memBytes[2];
        high = mem.memBytes[1];

  6. #6
    Registered User
    Join Date
    Aug 2011
    Posts
    5

    Thanks!

    Thanks. I am using am 8bit micro that is time critical. I am reading a large memory. It is a lot faster to grab bites out of a 32 bit int that it is to do all the bit shifting. Again thanks for your help. My C classes were in 1985.

  7. #7
    Registered User
    Join Date
    Aug 2011
    Posts
    5
    This works beautifully, thank you!

  8. #8
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    You're welcome. And just to bring you up to date a bit, C is largely the same language it was in 1985, but a good amount has changed too, and things have become much more standardized (there wasn't even a standard in 1985, the first one was in 1989). I recommend you peruse the C99 standard to see the current state of the language (it's a 12 year old standard, but it's still the standard). Most commercial compilers come close to the C99 standard, if they don't fully adhere to it. We have a draft copy here, which is just about as good as the real thing (the real one costs real money).

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Brucewd View Post
    Thanks. I am using am 8bit micro that is time critical. I am reading a large memory. It is a lot faster to grab bites out of a 32 bit int that it is to do all the bit shifting.
    Shifts are pretty fast. As much as I enjoy tinkering with unions, I doubt shifting is as slow as you think it is.


    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    Quote Originally Posted by quzah View Post
    Shifts are pretty fast. As much as I enjoy tinkering with unions, I doubt shifting is as slow as you think it is.


    Quzah.
    Wow, I never thought I'd meet someone who actually likes union-fiddling. I hate the rotten things. But it'd be a boring world if we were all alike.
    Code:
    while(!asleep) {
       sheep++;
    }

  11. #11
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by TheBigH View Post
    Wow, I never thought I'd meet someone who actually likes union-fiddling. I hate the rotten things. But it'd be a boring world if we were all alike.
    Haha.....quzah isn't what we would call "normal".
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  12. #12
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by quzah View Post
    Shifts are pretty fast. As much as I enjoy tinkering with unions, I doubt shifting is as slow as you think it is.


    Quzah.
    Some micro-controllers are very slow.
    And, some might have to really shift it 8 times per byte because of poor compiler optimization of the C code.

    Tim S.

  13. #13
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by TheBigH View Post
    Wow, I never thought I'd meet someone who actually likes union-fiddling. I hate the rotten things. But it'd be a boring world if we were all alike.
    You can do interesting things with them. For example, this bit of swapping bytes around with clearly labeled identifiers. For someone not used to shifting bits, it can give a clear indicator of what's going on.


    Quzah.
    Hope is the first step on the road to disappointment.

  14. #14
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Going back to the union idea...

    Code:
    union t_TRANSLATE
      { unsigned long int dword;
         struct byte
           { unsigned char b0;
             unsigned char b1;
             unsigned char b2;
             unsigned char b3; }}
         TRANSLATE;
    
       TRANSLATE.dword = 0xFAFAFAFA;
        printf("%d", TRANSLATE.byte.b2);
    Pretty simple really....
    Last edited by CommonTater; 08-01-2011 at 04:10 PM. Reason: fixed numbering...

  15. #15
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    What kind of crazy person starts numbering at 1?


    Quzah.
    Last edited by quzah; 08-01-2011 at 04:13 PM. Reason: tee hee
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 10-11-2010, 01:53 AM
  2. Folders in 8bit
    By epi_jimenez in forum C Programming
    Replies: 3
    Last Post: 04-10-2009, 10:44 AM
  3. 8Bit Bitmaps Problem
    By cboard_member in forum Game Programming
    Replies: 3
    Last Post: 08-19-2005, 05:55 PM
  4. Cross Platform Compatible 64Bit And 8Bit Integer
    By Geolingo in forum C++ Programming
    Replies: 5
    Last Post: 01-14-2005, 04:35 AM
  5. Converting a Long to chars[4]
    By Russell in forum C++ Programming
    Replies: 15
    Last Post: 12-18-2002, 06:33 PM