C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 10-23-2001, 08:28 AM   #1
DMaxJ
Guest
 
Posts: n/a
Question Array of boolean

void main()
{
boolean number[8] ;

}

is this the proper way to declare an array of boolean characters?


Thanks
  Reply With Quote
Old 10-23-2001, 08:40 AM   #2
Ethereal Raccoon
 
Procyon's Avatar
 
Join Date: Aug 2001
Posts: 189
Yes, except it's normally written as bool, not boolean.
Procyon is offline   Reply With Quote
Old 10-23-2001, 12:25 PM   #3
Unregistered
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!
  Reply With Quote
Old 10-24-2001, 08:24 AM   #4
electricglidingman
Guest
 
Posts: n/a
hey, could you please briefly (or not) explain how to do the bitpacking thing?
  Reply With Quote
Old 10-24-2001, 01:27 PM   #5
Confused
 
Magos's Avatar
 
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   Reply With Quote
Old 10-24-2001, 02:24 PM   #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   Reply With Quote
Old 10-24-2001, 04:04 PM   #7
Registered User
 
Aran's Avatar
 
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   Reply With Quote
Old 10-25-2001, 02:00 AM   #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   Reply With Quote
Old 10-25-2001, 02:10 AM   #9
Confused
 
Magos's Avatar
 
Join Date: Sep 2001
Location: Sweden
Posts: 3,122
Cool Wow

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   Reply With Quote
Old 10-25-2001, 02:19 AM   #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   Reply With Quote
Old 10-25-2001, 11:50 AM   #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   Reply With Quote
Old 10-25-2001, 11:45 PM   #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   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 12:13 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22