Results 1 to 7 of 7

Thread: Bit/Byte Manipulations in C

  1. #1
    Join Date
    Oct 2005
    Location
    Seattle, WA
    Beans
    494
    Distro
    Ubuntu 12.04 Precise Pangolin

    Bit/Byte Manipulations in C

    Greetings!

    I have a quick question that seems a bit difficult to google, or maybe I didn't try hard enough. Anyway, I want to know what happens if I have an unsigned short pointer (u16) and increment it on a unsigned char (u8) buffer.

    For example:
    PHP Code:
    unsigned short num;
    unsigned char buf[1024];
    num buf;
    num++; 
    Does this increment a whole 2 bytes? Would I go from byte 0 to byte 2?

    Thanks in advance!
    www.runtime-era.com - Blog About My Journey as a Developer

  2. #2
    Join Date
    Feb 2008
    Beans
    606
    Distro
    Ubuntu 11.04 Natty Narwhal

    Re: Bit/Byte Manipulations in C

    Quote Originally Posted by era86 View Post
    Does this increment a whole 2 bytes? Would I go from byte 0 to byte 2?
    Yes.

  3. #3
    Join Date
    Oct 2005
    Location
    Seattle, WA
    Beans
    494
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Bit/Byte Manipulations in C

    Sweet. And...

    When I cast a u16 to a u8, which bits are cut out of it? For example:

    PHP Code:
    u16 var16 0x00FF;
    u8 var8 = (u8)var16
    Do I get the 0x00 or the 0xFF?
    www.runtime-era.com - Blog About My Journey as a Developer

  4. #4
    Join Date
    Jun 2007
    Location
    Maryland, US
    Beans
    6,288
    Distro
    Kubuntu

    Re: Bit/Byte Manipulations in C

    era86

    Would it kill you to write a 10 line program to figure this out on your own?

    If you have a 16-bit value, why don't you tell me which portion of it you want to assign to your 8-bit value... do you want to MSB or the LSB? Depending on what you want, then assign your 8-bit value appropriately, without performing a "lame" cast. For example:
    Code:
    #include <stdint.h>
    ...
    uint16_t two_bytes     = 0xDEAD;
    uint8_t  one_byte_LSB  = two_bytes & 0xFF;             // obtains the LSB
    uint8_t  one_byte_MSB  = (two_bytes & 0xFF00) >> 8;    // obtains the MSB
    P.S.
    MSB = Most Significant Byte
    LSB = Least Significant Byte

    P.S.S.
    Code:
    #include <stdint.h>
    #include <assert.h>
    
    int main()
    {
      uint16_t value = 0xDEAD;
      assert((uint8_t)value == 0xAD);
    }
    Last edited by dwhitney67; March 6th, 2009 at 10:08 AM.

  5. #5
    Join Date
    Jul 2007
    Beans
    305
    Distro
    Ubuntu 9.04 Jaunty Jackalope

    Re: Bit/Byte Manipulations in C

    Quote Originally Posted by era86 View Post
    Sweet. And...

    When I cast a u16 to a u8, which bits are cut out of it? For example:

    PHP Code:
    u16 var16 0x00FF;
    u8 var8 = (u8)var16
    Do I get the 0x00 or the 0xFF?
    That depends on the endianness of the machine.
    iplist

    "Specialization is for Insects", R. Heinlein

  6. #6
    Join Date
    Feb 2008
    Beans
    606
    Distro
    Ubuntu 11.04 Natty Narwhal

    Re: Bit/Byte Manipulations in C

    Quote Originally Posted by uljanow View Post
    That depends on the endianness of the machine.
    No, you'll always get the low byte if you cast a u16 value to u8.

    I think you're confusing it with casting pointers, where the value of the byte at the address the u16 pointer pointed to would depend on the endianness.

    i.e.

    u16 foo
    u8* bar = (u8 *) &foo;

    *bar could be either high or low byte depending on the machine.

  7. #7
    Join Date
    Oct 2005
    Location
    Seattle, WA
    Beans
    494
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Bit/Byte Manipulations in C

    Quote Originally Posted by dwhitney67 View Post
    era86

    Would it kill you to write a 10 line program to figure this out on your own?

    If you have a 16-bit value, why don't you tell me which portion of it you want to assign to your 8-bit value... do you want to MSB or the LSB? Depending on what you want, then assign your 8-bit value appropriately, without performing a "lame" cast. For example:
    Code:
    #include <stdint.h>
    ...
    uint16_t two_bytes     = 0xDEAD;
    uint8_t  one_byte_LSB  = two_bytes & 0xFF;             // obtains the LSB
    uint8_t  one_byte_MSB  = (two_bytes & 0xFF00) >> 8;    // obtains the MSB
    P.S.
    MSB = Most Significant Byte
    LSB = Least Significant Byte

    P.S.S.
    Code:
    #include <stdint.h>
    #include <assert.h>
    
    int main()
    {
      uint16_t value = 0xDEAD;
      assert((uint8_t)value == 0xAD);
    }
    Thank you for your help. I thought someone might know on the top of their head, so I didn't bother to think if it would kill me to write a quick program. If it makes you feel better, I had figured this out on my own either way doing some very similar bit operations myself.

    Quote Originally Posted by movieman View Post
    No, you'll always get the low byte if you cast a u16 value to u8.

    I think you're confusing it with casting pointers, where the value of the byte at the address the u16 pointer pointed to would depend on the endianness.

    i.e.

    u16 foo
    u8* bar = (u8 *) &foo;

    *bar could be either high or low byte depending on the machine.
    Hmmm. I might look into this. I'm getting errors in my code and I do this alot. I'm guessing this is not good practice?
    www.runtime-era.com - Blog About My Journey as a Developer

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •