Thread: Array of boolean

  1. #1
    DMaxJ
    Guest

    Question Array of boolean

    void main()
    {
    boolean number[8] ;

    }

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


    Thanks

  2. #2
    Ethereal Raccoon Procyon's Avatar
    Join Date
    Aug 2001
    Posts
    189
    Yes, except it's normally written as bool, not boolean.

  3. #3
    Unregistered
    Guest
    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. #4
    electricglidingman
    Guest
    hey, could you please briefly (or not) explain how to do the bitpacking thing?

  5. #5
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145

    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.

  6. #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

  7. #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");
    }

  8. #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

  9. #9
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145

    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.

  10. #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

  11. #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.

  12. #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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. from 2D array to 1D array
    By cfdprogrammer in forum C Programming
    Replies: 17
    Last Post: 03-24-2009, 10:33 AM
  2. Replies: 7
    Last Post: 11-25-2008, 01:50 AM
  3. 1-D array
    By jack999 in forum C++ Programming
    Replies: 24
    Last Post: 05-12-2006, 07:01 PM
  4. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM