CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com

# Thread: Bit Operation in C++

1. Member
Join Date
Apr 2008
Posts
161

## Bit Operation in C++

I have three variables a,b,c of unsigned char (ie 1 byte size).So the maximmum value to hold is 0 - 255. My programming task is reduce the value in a with in 5 bits (0-31) ,value in b with 6 bits(0-63) and value in c with 5 bits (0-31). Then hold these 3 values (ie a,b,c) in a variable X of type unsigned short (size 16 bits) and write it to a binary file
How i achieve these data reduction using bit wise operation.......

2. Member
Join Date
Apr 2008
Posts
111

## Re: Bit Operation in C++

You can use C++ bitfileds http://msdn.microsoft.com/en-us/libr...be(VS.71).aspx

eg.

struct MyData
{
unsigned a: 5; // (5 bits)
unsigned b: 6; // (6 bits)
unsigned c: 5; // (5 bits)
}Test;

3. Member
Join Date
Apr 2008
Posts
161

## Re: Bit Operation in C++

Good Idea ...Thanks

4. Member
Join Date
May 2008
Posts
96

## Re: Bit Operation in C++

Except that using bitfields doesn't guarantee in what order they are packed.

Just bit-shift the values together:
Code:
```unsigned short pack565( unsigned char a, unsigned char b, unsigned char c ) inline
{
// with a in most-significant-bits, c in least-significant-bits
return ((a & 0x1F) << 11)
+((b & 0x3F) <<  5)
+ (c & 0x1F);
}```
If by "reduce" you mean that you are bit-packing RGB values, you'll need to do something other than simply truncate the most-signifcant-bits of your input. You'll need to scale the value down. A simple right shift is not off by more than one:
Code:
```unsigned short packRGB( unsigned char r, unsigned char g, unsigned char b )
{
// Again, r winds up in the most-significant-bits, etc.
return ((r << 8) & 0xF800)
+((g << 3) & 0x07E0)
+((b >> 3) & 0x001F);
}```
Hope this helps.

5. Member
Join Date
Apr 2008
Posts
161

## Re: Bit Operation in C++

On the above two methods ie by bit field and by bit operation which is best and error free when use with fread () and fwrite ()...

6. Member
Join Date
May 2008
Posts
96

## Re: Bit Operation in C++

Both work just fine. The "best" one is the one that fits your requirements.

What exactly are you trying to do? (What is your data?)

7. Elite Member
Join Date
Oct 2006
Location
Sweden
Posts
3,654

## Re: Bit Operation in C++

Duoas method works best if you need control over what bits within the 16-bit are utilized for each subpart. In other words (just as the ms link says). For one compiler the declaration
Code:
```struct MyData
{
unsigned short a: 5; // (5 bits)
unsigned short b: 6; // (6 bits)
unsigned short c: 5; // (5 bits)
}Test;```
pack data in the order a/b/c, another compiler will pack in the order c/b/a (hopefully to 16-bit but even that is no guarantee for all platforms). Also, using unsigned short as I did is non-standard. The standard say that int (or unsigned int) shall be used and that means that in a 32-bit machine the result will take four bytes instead of three bytes.

Regarding safety and fread/fwrite, if those files are handled by machines that have the same endianess (and the compilers work the same for all platforms) there should be no practical difference.

8. Member
Join Date
Apr 2008
Posts
161

## Re: Bit Operation in C++

Both methods work properly
Thanks to all !

#### 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