#### Archived

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

# next question: bitfields...

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

## Recommended Posts

to type << type : ampersand lt &<
oh shit! all my 2 << something, should be 1 << something

1 << value is the same as 2^value

##### Share on other sites
so you''re saying that if i want to set bit 3 to the bit variable,
i''ll have to do this:

int bitnm = 3;
bit = 1 << bitnm;

and to get it, you would say

if(bit &= 1 << bitnm) // do whatever

am i right?

Yep that''s true

##### Share on other sites
...or just
if (bit & (1 << 3))

you don't NEED to assign it to a variable.

BTW, this statement doesn't make sense:
if (bit &= (1 << bitnum))

The "&=" operator will clear all bits in bit except 1 << bitnum. Usually, when you're testing a value, you use the operators & and /.

See, your statement there is kinda like saying:
if (a = b)

Now, this is legal, but what it really translates to is:
a = b; if (b)

bit &= (1 << bitnum); if (bit & (1 << bitnum))

Hope that hasn't confused you more.

As always, you should just write up some code that compiles and step through it with the debugger . Switch display to hex mode, it makes it easier with bitfields. Good luck.

Edited by - Stoffel on June 16, 2000 6:53:38 PM

##### Share on other sites
I'll give a little explanation that will hopefully make some sense of what's going on.

Basically to check a specific bit you AND it with a number that only has that specific bit you want to check set. Examples of these include:

1 - only bit 0 is set
2 - only bit 1 is set
4 - only bit 2 is set
8 - only bit 3 is set
16 - only bit 4 is set
128 - only bit 7 is set
32768 - only bit 15 is set
2147483648 - only bit 31 is set

And that is where the shifting comes in. Look at the number 1 represented as a byte:

00000001

If you binary shift to the left once the number then becomes:

00000010 or 2

Shift by 2 and it becomes

00000100 or 4

etc., etc.

That's how you get your number with only one bit set.

Anyway, let's look at the operation within the if statement that the people have been posting up:

number1 & number2

This checks them bit by bit. Only if both bits are set will the resulting bit be set. If both are 0 or only 1 is set, the resulting bit is 0.

Let's say number1 = 3 and number2 = 6.

In binary:

3 = 0011
9 = 1001
result = 0001 or 1.

Now an actually useful example: Let's say we use a number that only has one bit set like, say, the number 16. Only bit 4 is set. Let's say number1 = 22 and number2 = 16 because we want to check if bit 4 is set in 22:

22 = 10110
16 = 10000
result = 10000 or 16

Bit 4 was set so the result is non-zero so just like any if statement if the result is non-zero then success.

Now let's use a number like 359 where 4 bit is not set:

359 = 101100111
16 = 000010000
result = 000000000 or 0

In an if statment 0 obviusly means false and that one bit set in the 2nd number is the only place that could possibly change the value above 0 which would make the if statemnet true as all bits of the 2nd number except the one you want to check would be 0 and you have to have 2 1's to get 1. In conclusion to check a bit you do this:

if(number & (1 << bit_place) )
//bit is set

Now let's look at setting a bit. You bitwise OR a number by a number that only has one bit set (as above just ORing it this time) and store the result in a new number. With ORing the only time the resulting bit is 0 is if no bits have been set. Using the same numbers as above:

359 = 101100111
16 = 000010000
result = 101101111 or 375

To safeguard against setting a bit we don't want to set and keeping bits not set, we have a number with zero everything except the bit we want set. So if the bit is 1 in the original number it will stay 1 because the other bit will be zero. If it is zero it will stay zero because 2 zeros = 0. Then when it hits the 1 in the 2nd number it will change the bit to 1 if was 0 or just leave it at 1 if is already one because 2 zeros = 0; 1 and 0 = 1.

To set a bit:

number /= (1 << bit_place)

Now to zero a bit you XOR it. XORing results in a 1 bit when one and only bit is set.

359 = 101100111
16 = 000010000
result = 101100111

Same princable as above except when it hits a 1 in the 2nd number it will zero the coresponding bit if it is set to 1 because 2 1's = 0 when XORing. It will stay 0 if it was originally 0 because 1 and 0 = zero with XORing.

To clear a bit:

number ^= (1 << bit_place)

Damn, that was long. Hope I made some sense in all that rambling...

Edited by - Sheltem on June 17, 2000 7:05:27 AM

##### Share on other sites
okay, i think i understand now, but one big question I have is, how many bits can you hold in an int? say i have 20 different bit''s i''ll need to use for stuff in the game, will i have to have 2 different int variables?

sorry for buggin again, but just thought i''d ask...

david

##### Share on other sites
Because different processors handle different sizes of data better depending on the size of the processor(or data bus to be more exact), int is not absolutly defined in C. int can either by 2 bytes/16 bits or it can be 4 bytes/32 bits. If you want to explicitly declare a integer that is 2 bytes long you declare with 'short' instead of int. If you want it to be 4 bytes you declare it as 'long'. When you use 'int' you are using whatever the maker's of the compiler you are using defined int as. You'll have to look in the docs of the compiler to know for sure. In old DOS compilers, int is usually defined as 2 bytes because the target processors and the OS were 16-bit. With Windows compilers, int is usually defined as 4 bytes because the target OS and processor are 32-bit.

Edited by - Sheltem on June 17, 2000 11:48:53 PM

##### Share on other sites
I''ve never come across a system that doesn''t use 32 bit ints, but then again I haven''t had that much experience yet . Just use long int if you don''t feel secure.

- IO Fission

Tearing 'em apart for no particular reason...

##### Share on other sites
I''m sorry, but those aren''t C++ bitfields.

I''m not sure about the performance of this, but look at how easy it is:

struct flags{bool hardware : 1; // uses just 1 bitbool does_3D : 1; // also uses just 1 bit};void main(){// allocate 2 bits of memoryflags my_flags;// access the flagsflags.hardware = true;flags.does_3D = false;}

Note that you should only use bitfields like this when size is of utmost importance (like in compressing files). Using anything not aligned on the sizeof(int) will most likely slow the operation down a bit. So don''t go crazy with bitfields; just use them where you need them.

- null_pointer
Sabre Multimedia

##### Share on other sites
Bitfields use a whole integer when you don''t have one... I mean this:

  // I love those Sources... :-)typedef struct _TOTO{ int bfield:1; int bfield2:1;} Toto, *pToto;// If the compiler works well, the following will happen:Toto Tata; // Only 1 short value taken for both bfields.// In some old compiler or free compilers... it will take 1// short integer for each bfields.// Now the problem is the following:Toto Popo[800];// It will take 2800 short value integers (16 bits)...// It won''t take 100. Why? Because it allocates 800// structures of a size of 16-bits.

I have explained a way to avoid this in another post (named "3 Bit interger")... It uses simple C calls and it is not optimized...
So if you want 3 bits fields array to take less space (which could be the case of a 2800x1600 array of only 3 bits fields), check it out. It can be changed simply to fit your needs.

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).

1. 1
Rutin
42
2. 2
3. 3
4. 4
5. 5

• 9
• 27
• 20
• 14
• 14
• ### Forum Statistics

• Total Topics
633387
• Total Posts
3011608
• ### Who's Online (See full list)

There are no registered users currently online

×