![]() |
| | #1 |
| Guest
Posts: n/a
| { boolean number[8] ; } is this the proper way to declare an array of boolean characters? Thanks |
|
| | #2 |
| Ethereal Raccoon Join Date: Aug 2001
Posts: 189
| Yes, except it's normally written as bool, not boolean. |
| Procyon is offline | |
| | #3 |
| Guest
Posts: n/a
| 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! |
|
| | #4 |
| Guest
Posts: n/a
| hey, could you please briefly (or not) explain how to do the bitpacking thing? |
|
| | #5 |
| Confused Join Date: Sep 2001 Location: Sweden
Posts: 3,122
| Reply 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. |
| Magos is offline | |
| | #6 |
| Registered User Join Date: Sep 2001
Posts: 164
| 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 |
| gliptic is offline | |
| | #7 |
| Registered User Join Date: Aug 2001
Posts: 1,301
| 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");
}
|
| Aran is offline | |
| | #8 |
| Registered User Join Date: Sep 2001
Posts: 164
| 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 |
| gliptic is offline | |
| | #9 |
| Confused Join Date: Sep 2001 Location: Sweden
Posts: 3,122
| 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. |
| Magos is offline | |
| | #10 |
| Registered User Join Date: Sep 2001
Posts: 164
| 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 |
| gliptic is offline | |
| | #11 |
| Blank Join Date: Aug 2001
Posts: 1,034
| 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. |
| Nick is offline | |
| | #12 |
| Registered User Join Date: Sep 2001
Posts: 164
| 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 |
| gliptic is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| from 2D array to 1D array | cfdprogrammer | C Programming | 17 | 03-24-2009 10:33 AM |
| Help with Searching Array for Longest Sequence of an Element | w2look | C Programming | 7 | 11-25-2008 01:50 AM |
| 1-D array | jack999 | C++ Programming | 24 | 05-12-2006 07:01 PM |
| Class Template Trouble | pliang | C++ Programming | 4 | 04-21-2005 04:15 AM |
| Help with an Array | omalleys | C Programming | 1 | 07-01-2002 08:31 AM |