Skip to main content
GameDev.net gamedev.net
🔒 Locked

Low level strategy

Started by tifkagh Mar 18, 2003 at 3:39 PM 5 replies 600+ views
Original Post
tifkagh
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?
cgoat
cgoat
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
}

Beer Hunter
Beer Hunter
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?
falkone
falkone
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]
Disclaimer: "I am in no way qualified to present advice n any topic concerning anything and can not be held responsible for any damages that my advice may incurr (due to neither my negligence nor yours)" o
jesterlecodeur
jesterlecodeur
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
_______________
Jester, studient programmerThe Jester Home in French

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.