void main()
{
boolean number[8] ;
}
is this the proper way to declare an array of boolean characters?
Thanks
void main()
{
boolean number[8] ;
}
is this the proper way to declare an array of boolean characters?
Thanks
Yes, except it's normally written as bool, not boolean.
Arrays of bools are horibly inefficient. Use bitpacking instead. Each bool is a byte, while a byte can be used to store 8 boolean values as bits. That's a size savings of 8:1!
hey, could you please briefly (or not) explain how to do the bitpacking thing?
unsigned char Number;
(Number & 1) Get 1:st bit out of Number
(Number & 2) Get 2:nd bit out of Number
(Number & 4) Get 3:rd bit out of Number
(Number & 8) Get 4:th bit out of Number
(Number & 16) Get 5:th bit out of Number
(Number & 32) Get 6:th bit out of Number
(Number & 64) Get 7:th bit out of Number
(Number & 128) Get 8:th bit out of Number
Ex:
if(Number & 1) cout << "First bit is true";
if(!(Number & 4)) cout << "Third bit is false";
MagosX.com
Give a man a fish and you feed him for a day.
Teach a man to fish and you feed him for a lifetime.
Setting a bit:
(Number | 1) Set 1:st bit in Number
(Number | 2) Set 2:nd bit in Number
(Number | 4) Set 3:rd bit in Number
(Number | 8) Set 4:th bit in Number
(Number | 16) Set 5:th bit in Number
(Number | 32) Set 6:th bit in Number
(Number | 64) Set 7:th bit in Number
(Number | 128) Set 8:th bit in Number
Toggle a bit:
(Number ^ 1) Toggle 1:st bit in Number
(Number ^ 2) Toggle 2:nd bit in Number
(Number ^ 4) Toggle 3:rd bit in Number
(Number ^ 8) Toggle 4:th bit in Number
(Number ^ 16) Toggle 5:th bit in Number
(Number ^ 32) Toggle 6:th bit in Number
(Number ^ 64) Toggle 7:th bit in Number
(Number ^ 128) Toggle 8:th bit in Number
In assembler, you can do much more efficient bitpacking but I guess most of you don't care about assembler (C++ board).
// Gliptic
umm.. how would you declare that and use it? I'm looking to use a similar idea for a project for school...
would it be like
Code:main() { int a; // sets first to true a ^ 1 // all the rest are false still if (a | 1) printf("a is true"); else printf("a is false"); }
No, the '^' is XOR. It toggles bits so you must use '|' to set it and you must write:
a ^= 1;
and not just 'a ^ 1' because that is removed by your compiler because it doesn't do anything.
The rest of the bits in 'a' can be anything. You have to initialize it to 0 to be sure it is 0.
This loop sets all the bits in a byte:
Code:unsigned char a; long b = 1; //The lowest bit is set for(long i = 0; i < 8; i++) { a |= b; //Set the bit b <<= 1; //Go to the next bit }
// Gliptic
Wow! I have never seen the 'b<<=1' operator before. Cool! Multiplies with 2, right?
MagosX.com
Give a man a fish and you feed him for a day.
Teach a man to fish and you feed him for a lifetime.
Yes, it's the same as moving all bits to left one step.
(00001 << 1) = 00010
In that way I can set all the bits in the byte.
// Gliptic
Well
0x00000001 XOR 0x000000001 is 0x00000000
I think it's about time
we stop using hex and start using 32 base.
With 64 bits comming up, hex is just as tedius to
write as binary was with 8 bits.
Base 32 ? You mean like splitting up the numbers into small 5-bit parts? Why 5-bit? Isn't it better to use base 256?
// Gliptic