Jump to content
  • Advertisement

Archived

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

neonstar

next question: bitfields...

This topic is 6749 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

BTW, for bitting (ok mischosen term...), I always cast my integers either long or short... Rarely I use the "int" keyword because I want to keep a control over that kind of problem. Though most of the computers nowadays take 32-bits integers, sometimes I prefer to put short integers (when I''m having values that cannot pass through 65535). But almost all of the time I use simple long values, because anyway the processors handle them in one cycle. That was my two cents.

(( Applaud Toto, Tata and Popo who came handly once again for a wonderful example ))

Programming is:
A.The art of debugging a blank sheet of paper (or an empty file).
B.A pastime similar to banging one's head against a wall, but with fewer opportunities for reward.
C.The most fun you can have with your clothes on (although clothes are not mandatory).

Share this post


Link to post
Share on other sites
Advertisement

hey, thanks for all the help everyone. eventually i figured out what my problem was on my own last night, it seemed i was doing something wrong when i was checkin for status of a bit. look at this:


if(TileBits &= 1<
i had that equal sign in there and it was setting the bit rather than checking it... geez i feel dumb... oh well, that''s just part of the game, i guess...

thanks,
david

Share this post


Link to post
Share on other sites
Poltras: You can have the compiler pack it to whatever size you wish, and you should even be able to use 2 bits. Use the compiler-specific packing preprocessor commands. VC uses #pragma pack. You can also pad it manually if you wish -- that's part of C++. Here is a console app that shows the alignment:

        
#include "stdafx.h"
#include <iostream>

using namespace std;

#pragma pack(push)

struct flags_2
{
bool hardware : 1;
bool does_3D : 1;

// no padding

};

#pragma pack(pop)

struct flags_8
{
bool hardware : 1;
bool does_3D : 1;

char : 0; // pads to char alignment

};

struct flags_16
{
bool hardware : 1;
bool does_3D : 1;

short : 0; // pads to short alignment

};

struct flags_32
{
bool hardware : 1;
bool does_3D : 1;

long : 0; // pads to long alignment

};

struct flags_optimal
{
bool hardware : 1;
bool does_3D : 1;

int : 0; // pads to int alignment

};

int main(int argc, char* argv[])
{
// sizeof doesn't work with bitfields;

// the smallest quantity it can return

// is sizeof(char) or one byte

cout << "flags_8 takes up " << sizeof(flags_8) << " bytes" << endl;

cout << "flags_16 takes up " << sizeof(flags_16) << " bytes" << endl;

cout << "flags_32 takes up " << sizeof(flags_32) << " bytes" << endl;

cout << "flags_optimal takes up " << sizeof(flags_optimal) << " bytes" << endl;

return 0;
}






- null_pointer
Sabre Multimedia


Edited by - null_pointer on June 19, 2000 8:54:06 AM

Share this post


Link to post
Share on other sites
How can it not have any padding when it''s using only 2 bits? Isn''t the smallest addressable unit on a PC a byte? And how would you read/write these bitfields to disk?

Share this post


Link to post
Share on other sites
hmm...I guess the smallest unit is 1 byte. If you use C++ bitfields, then you can choose your padding according to your needs.

To go about reading/writing bits to a file (like you would in a compression program), you hold a cache (like 4 ints?) and cache the bit read/write operations until they fill up the ints and then read/write the ints to disk. In C++, you can also do unions if you like to make the code more readable:

    
#include <iostream>

using namespace std;

// make the code more readable

const bool on = 1;
const bool off = !on;

// use a packet of 32 bits

union packet
{
unsigned long number;

struct
{
unsigned char bit_01 : 1;
unsigned char bit_02 : 1;
unsigned char bit_03 : 1;
unsigned char bit_04 : 1;
unsigned char bit_05 : 1;
unsigned char bit_06 : 1;
unsigned char bit_07 : 1;
unsigned char bit_08 : 1;
unsigned char bit_09 : 1;
unsigned char bit_10 : 1;
unsigned char bit_11 : 1;
unsigned char bit_12 : 1;
unsigned char bit_13 : 1;
unsigned char bit_14 : 1;
unsigned char bit_15 : 1;
unsigned char bit_16 : 1;
unsigned char bit_17 : 1;
unsigned char bit_18 : 1;
unsigned char bit_19 : 1;
unsigned char bit_20 : 1;
unsigned char bit_21 : 1;
unsigned char bit_22 : 1;
unsigned char bit_23 : 1;
unsigned char bit_24 : 1;
unsigned char bit_25 : 1;
unsigned char bit_26 : 1;
unsigned char bit_27 : 1;
unsigned char bit_28 : 1;
unsigned char bit_29 : 1;
unsigned char bit_30 : 1;
unsigned char bit_31 : 1;
unsigned char bit_32 : 1;
};
};

int main(int argc, char* argv[])
{
packet my_packet;

// use it like a long

my_packet.number = 5890327;

// access the individual bits

my_packet.bit_01 = off;
my_packet.bit_07 = on;
my_packet.bit_27 = on;

cout << "my_packet = " << my_packet.number;

return 0;
}







- null_pointer
Sabre Multimedia

Share this post


Link to post
Share on other sites
Ah, I see. Thanks alot for the info. But I'm having a problem reading bitfields from a file. Here is what I have:

        

#include <iostream.h>
#include <fstream.h>

const bool on = 1;
const bool off = !on;

union bitfield
{

unsigned char bits;
struct
{

bool bit1 : 1;
bool bit2 : 1;
bool bit3 : 1;
bool bit4 : 1;
bool bit5 : 1;
bool bit6 : 1;
bool bit7 : 1;
bool bit8 : 1;

};
};


int main()
{

bitfield bitfield_write;
bitfield_write.bits = 0;

bitfield_write.bit1 = on;
bitfield_write.bit3 = on;

ofstream output_file("file.txt");
output_file << bitfield_write.bits;

bitfield bitfield_read;
bitfield_read.bits = 0;

ifstream input_file("file.text");
input_file >> bitfield_read.bits;

return(1);

}




It correctly writes to the file but bitfield_read always stays at 0, after the byte extraction process. I've also tried:

bitfield_read.bits = input_file.get();
input_file.read((char*)bitfield_read.bits, 1);

The 1st one changes the value to 255 (where it should be 5). The other leaves it at 0. Any ideas? Thanks again.

Edited by - Sheltem on June 20, 2000 8:44:42 PM

Share this post


Link to post
Share on other sites
OK, first the output filename and input filename are different (probably just a typo). Second, the file you are writing is in text mode, so I think you must append an endl to the file when you write it out, like this:


output_file << bitfield_write.bits << endl;



It reads and writes fine now.

Good Luck!





- null_pointer
Sabre Multimedia

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!