next question: bitfields...

Started by
25 comments, last by neonstar 23 years, 10 months ago
thanks everyone for such a quick response on the last one. this one i''m sure will be equally as easy... i''m trying to implement bitfields in my code for my tiles so that it will be expandable at runtime. i need to know the most efficient way to do bitfields in my code. currently, the way i have it is an integer variable that gets assigned the bit number like so bit &= bitnum; to remove the bit it''s bit &=~ bitnum; what am i doing wrong? david
--david@neonstar.netneonstar entertainment
Advertisement
Hey there again

first of all (I hope you already did that) you need to shift the value, so it really will be the bit you mean

if you want to do something with only the third bit (bit 2), then you'll be working with bitnum = 2 << 2;

Setting a bit: bit /= bitnum
Removing a bit: bit &= ~bitnum
Checking if set: bit ^ bitnum
Togglinh a bit (1->0, 0->1): bit ^= bitnum

And keep up asking messages so I become dedicated ))



Edited by - Jrz on June 16, 2000 12:21:04 PM

okay jrz, i''m gonna go in-depth here.

say for instance in my last post, i have that dynamic char array. i''m loading in my info from a text file. the first line of the file holds the number of bits inside of the file. the code then parses the next lines of the file until it reaches the end result bit number. i''ve got all of that to work correctly, except for the fact i''d like to make my parser skip empty lines and lines that start with ''#'' (but that is a whole ''nother post in itsself... ;-) now the way i use that information is just by using the array index number of the certain bit (i scan the char array with strcmp() for the certain bit name and return the corresponding array index). just for your knowledge, i skip the first (0) variable in the array so the bits will start off at 1...

now for implementation, i have a list of bits that goes from say 1-5. for example bit number 3 would be BLOCKSMOVEMENT. when i scan for that bit for the current tile inside of my engine code, i''m looking for bit 3. how does all of the bit shifting (<<) come in to play with that? i really never understood it. if you could, could you give me a bit of an example how i would set and obtain bit 3 from a certain integer? i know you''re just itchin'' to get your status us Jrz, so fix this one for me too!

thanks a lot,
david
--david@neonstar.netneonstar entertainment
bah, just getting my post back on top of the list...

anyone got an abswer?
--david@neonstar.netneonstar entertainment
hmmm i meant answer, not abswer...

dave
--david@neonstar.netneonstar entertainment
Ok, I'll explain some bit stuff here

a byte is 8 bits long (or short ), but you already knew that

say you have a variable that has value 8

v = 8;
in memory, this would be coded like:
-8 bits-
00000100

bit 0(the most right one) is 2^0 = 1
bit 1 is 2^1 = 2
bit 2 is 2^2 = 4
etc
you knew that i think

now the bit shifting
when you multiply value by 2 (10 if you use our decimal way)
the bits will shift to the left
38.000 * 10 = 3800.00 this is the way we count
00000100 * 2 = 8 * 2 = 16 = 00001000 shift logical left

if you shift the bits 2 places, you multiply with 4
for 3 places with 8

now, I hope I still make sense here
if I want to see if a value has bit 3 set
for example: 83 = 01010011
x <- this is bit 3

then we need to see if (83 & 8) is true (8, because it's 2^3)

the shifting is just a faster way to do powers of 2

it's also nice to have some #defines in your program

#define bit0 1
#define bit1 2
#define bit3 4
#define bit4 8
etc
and
#define bit(x) (2 << x)

this will keep your code clean, I hope

well.. I know, this was not very userfriendly but I just wrote down what came in my mind





Edited by - Jrz on June 16, 2000 3:40:45 PM

Edited by - Jrz on June 16, 2000 3:41:12 PM
okay, now i''m insanely confused... i still don''t understand the need to shift the bits.... is my bit variable supposed to be a BYTE type or can it just be a standard int? i hate this stuff, it makes me feel dumb... ;-)

thanks for the info,
dave
--david@neonstar.netneonstar entertainment
You can stor 8 bits in a byte, 16 in an int
depends on your needs
But it should be called flags, flag or bits.


Edited by - Jrz on June 16, 2000 4:06:54 PM
that''s not really what i mean...

i need to know whether my bit variable is

BYTE bits;
or
int bits;

and for the record, shifting with the indirection operator (<<) makes the value transformed into a ''0001010'' type variable?

bah, this stuff sucks...

dave
--david@neonstar.netneonstar entertainment
also, what happens when you go

bit = 1 << array_bit_number;

i've seen something like that done before and it worked, i just didn't understand it to implement it into my code.



Edited by - neonstar on June 16, 2000 4:22:29 PM
--david@neonstar.netneonstar entertainment

This topic is closed to new replies.

Advertisement