Archived

This topic is now archived and is closed to further replies.

tifkagh

Low level strategy

Recommended Posts

As far as I can see my C++ implementation (gcc version 3.2 Mandrake Linux) regards a char as 4 bytes in length. Needless to say this is not the most convenient way to get at individual bits. What strategies to gamedev regulars use for addressing individual bits in C++ code? Is the bitset template worth considering?

Share this post


Link to post
Share on other sites
There''s no way it treats a char as 4 bytes. The standard requires that a char be one byte. Although if that''s not true, then someone will correct me.

Anyway, use the bitwise operators &,|,^,~ and hex values to work with individual bits, for example:
const int BIT1 = 0x01;
const int BIT2 = 0x02;
const int BIT3 = 0x04;

unsigned int bitfield;

bitfield = (BIT1 | BIT3); // Set bits 1 and 3
if(bitfield & BIT1) // Is bit 1 set?
{
// Do something
}
if(bitfield & BIT2) // Is bit 2 set?
{
// Do something else
}

Share this post


Link to post
Share on other sites
quote:
Original post by cgoat
The standard requires that a char be one byte. Although if that''s not true, then someone will correct me.
If you aren''t sure, why are you saying it in the first place?

Share this post


Link to post
Share on other sites
You can use bit fields.. the union is to enable easy access of the value or the bits..

EDIT: Oh, and about the 4-bytes.. would a UTF encoding make a difference?


    
struct byte_t
{
union
{
struct
{
char bit1 :1;
char bit2 :1;
char bit3 :1;
char bit4 :1;
char bit5 :1;
char bit6 :1;
char bit7 :1;
char bit8 :1;
};
char bytevalue;
};
};


[edited by - falkone on March 18, 2003 5:02:57 PM]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
I don''t think that char has to be 1 byte, although that is the case on pretty much every compiler I''ve ever heard of.

Share this post


Link to post
Share on other sites
quote:
Original post by tifkagh
As far as I can see my C++ implementation (gcc version 3.2 Mandrake Linux) regards a char as 4 bytes in length. Needless to say this is not the most convenient way to get at individual bits.

What strategies to gamedev regulars use for addressing individual bits in C++ code?

Is the bitset template worth considering?


try a sizeof(char), maybe you use a compiler option? Are you compiling for 32bit unicode?

Standart say only that''s it must be large enough to support caracter set.

Consider using boost::int8_t http://www.boost.org

Share this post


Link to post
Share on other sites