Advertisement

initlializing variables with binary value

Started by February 21, 2000 02:25 PM
9 comments, last by Ridcully 24 years, 7 months ago
simple question: how can i assign a binary value to a variable? for example i can assign a hexadecimal value this way: BYTE b = 0xFF; so how can i do the same with binary values without converting it by hand into decimal? thanks ridcully oh, i am using C++ of course Edited by - Ridcully on 2/21/00 2:26:26 PM
just use a calculator, it''s really not that difficult
-werdup-
Advertisement
Well, when you assign hex to a variable it''s being stored as 1s and 0s(you might argue, but just wait). What I think you meant to ask is how you can compare it to a binary value to see if bit 0 or bit 1 is set or something. Well, there is no way to declare a number binary, like you can hexadecimal 0xFF. Instead, you have to understand somewhat how bits are stored, and you''re binary operations.

If you want to test if bit 1 of a char testvar is set, you would do it like this:
if (testvar / 1)
/* Do whatever */
to test each bit, you OR it with a power of two. For example, if you want to test bit 1 then it''s 1^2 if you want to test bit 7, you would OR the variable with 7^2.

You also need to know how to set bits individually. You would use the bitwise AND operator like this:
testvar = testvar & 1;
or, that could be simplified with:
testvar &= 1;

Finally, you need to now how everything is organized in a char. The highest bit is on the left, and is called bit 7 while the lowest bit is farthest on the right and called bit 0.
Bit order in char:
[ 7 6 5 4 3 2 1 0 ]
In ints it''s the same way, except the farthest left is a 15 instead of a 7.

Well, hopefully that helped you, and if not I hope it was at least a little insight for what you can do in programming. These are normally used lots in games, to save space and make external world files easier to implement. In case you''re wondering this method is called bitvectors. Hope that helped some!
thanks, but you know calculating something like that:
1111100000000000 makes me somehow weary. and that's only 16bit

so how can i tell the compiler to do that for me? prolly something like:
__int16 = 0b1111100000000000;
?

anyway thanks for the input

Edited by - Ridcully on 2/21/00 3:03:39 PM
Actually now that i think about it, I''m pretty sure you can just append a lowercase b to the number, and it''ll assume it''s binary, like this:
testvar = 10101010b;
but, i''m not sure - you might have to experiment a little bit. Once again, hope that helped!
ah, thanks, i''ll try that asap...
that''s excactly what i am searching for.

oh, do you incidentally know how big a BOOL var is stored in the memory? i can''t believe it is just one bit, so i think it''s just a int used with a structure, am i right?
Advertisement
Unfortunately, I''m a C programmer, and don''t mess around much with C++. A BOOL cannot be just 1 bit, just because of the x86 architecture. But, depending on how bool is implemented, they may have made it so that when you make several BOOLs, they will all be stored in the same byte, to save memory. That''s just an educated guess on my part, and it sounds good....anyway, good luck .
I know that VC 6 has a one-byte bool (yes, in lowercase); other versions of VC used 4 bytes. I believe in windows.h BOOL is declared something like this:


#define BOOL int


Your compiler should have documentation on it though.

Good Luck!


- null_pointer

To assing a binary value to a variable, you can use this syntax :

int x = 00100100101b;

The bool (lowercase) type is part of the current ANSI C++ specs, but I''m pretty sure it doesn''t uses only 1 bit of storage space. I think it uses 8 bits ( or one char ).






If you are not going to use a class, do the following.

#define SET_BIT(bit, bit_flag) ((bit) &= (bit_flag))
#define REMOVE_BIT(bit, bit_flag) ((bit) &= ~(bit_flag))

Use those and set up other defines to detail what the flags stand for. (i.e.)

#define MONSTER_ANGRY (1<<0)
#define MONSTER_FIGHTING (1<<1)

The shift operator(I think I have it the right way) will define your flags and the macros above(again I think they are syntax correct) will help you set and unset bits. You can make an IS_SET(bit, bit_flag) macro as well to test if a bit is set to help you out as well.

Now if you do this in a class, use inline functions so that you don''t have too much class overhead for something that is relatively faster than int operations. Also remember the size of the bitflag your working with. You will shoot yourself later if you need more than the variable can hold and have to change the implementation of it.

Kressilac

Derek Licciardi (Kressilac)Elysian Productions Inc.

This topic is closed to new replies.

Advertisement