CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Feb 2007
    Posts
    90

    Copy Bits To a Byte

    Hello all,

    lets say that I have 3 unsigned chars.

    Code:
    unsigned char bitContainer = 0;
    unsigned char bitValues[2];
    What I wanna do is this:
    I want to copy the last 5 bits of the variable bitValue[0] and the first 3 bits of the bitValue[1] into bitContainer. The first 5 bits of bitContainer are the last 5 bits of the bitValues[0] and the last 3 bits of bitContainer are the first bits of bitValues[1].

    Thanks and ,
    Best Regards,
    BoSCHoW

  2. #2
    Join Date
    Jun 2005
    Posts
    315

    Re: Copy Bits To a Byte

    Something similar to this?
    Code:
    bitContainer = bitValues[1] << 5;
    bitContainer |= bitValues[0] >> 3;

  3. #3
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: Copy Bits To a Byte

    Just a note. It's a lot better to always talk about most/least significant bits/parts of a value.

    Jeron's post assumes that the last bits is the most significant bits. Does that correspond with what you think BoSCHoW?

  4. #4
    Join Date
    Feb 2007
    Posts
    90

    Re: Copy Bits To a Byte

    Yes this is exactly how I was thinking. And I have managed to find a solution to my problem. Thanks again.

    Best regards,
    BoSCHoW.

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured