Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

Boolean variables in C

Name: Anonymous 2010-05-30 12:44

Has anyone created a method for using a byte as 8 boolean variables in C? I could implement it myself, but I'm curious, as it seems that the usual is to use a whole int for logical values. What I mean is something like this:

bool a;
bool b;
bool c;
bool d;
bool e;
bool f;
bool g;
bool h;
bool i;


And from a to h it would use just one byte, but when it's going to allocate memory for i it would the first bit from another byte. Also, is this how C++ implements its boolean types, or does it use a integer per boolean variable?

Name: Anonymous 2010-05-30 12:46

>>1
C struct bitfields.

ALternatively, Common Lisp bitvectors.

Or just make them yourself, it's rather trivial ( to check if a flag is set or not is easy: (1 << flag_number) & bitfield )

Name: Anonymous 2010-05-30 12:49

I'd leave it up to my compiler, but then again, I'm not insane.

Name: >>2 2010-05-30 12:50

Other people also prefer to cache the 1 << flag_number like this:


#define FLAG_1  0x00000001
#define FLAG_2  0x00000002
#define FLAG_3  0x00000004
#define FLAG_4  0x00000008
#define FLAG_5  0x00000010
...
#define FLAG_32 0xF0000000

And they can just do if (flags & FLAG_n) ... to test for a specific flag. flags |= FLAG_n; to set it.

Name: sage 2010-05-30 13:14

>>1
Has anyone created a method for using a byte as 8 boolean variables in C?

First of all, 10/10 IHBT, A+ would be trolled again etc.

Secondly... People have been doing this for the entire history of computer programming.  I hope that you are a relatively new programmer, otherwise I will have to slap you for not knowing that everyone already started doing this decades ago.

#include <stdio.h>
#include <stdint.h>

#define BITSET(v, bit)          (v |= (1 << bit))
#define BITCLEAR(v, bit)        (v &= ~(1 << bit))
#define ISBIT(v, bit)           (v & (1 << bit))
#define BITTOGGLE(v, bit)       (v ^= (1 << bit))

int main(void) {
    uint8_t n = 0;
    if (ISBIT(n, 0)) { // false
        printf("Bit 0 is set in the value %i.\n", n);
    }
    BITSET(n, 0);
    if (ISBIT(n, 0)) { // true
        printf("Bit 0 is set in the value %i.\n", n);
    }
    n = 255;
    BITTOGGLE(n, 7);
    printf("255, but with bit7 toggled off == %i\n", n);
    return 0;
}

Name: Anonymous 2010-05-30 13:24

>>5
Yes, I'm new to programming.

Thank you all for your help.

Name: >>2 2010-05-30 13:32

>>5
It's older than the history of computer programming. It was first done in hardware design.

Name: Anonymous 2010-05-30 13:33

>>7
Yeah, probably.

It's alright not to know if you're new to programming.

Name: Anonymous 2010-05-30 14:24

Name: Anonymous 2010-05-30 15:03

Name: Anonymous 2010-05-30 15:20

>>10
Is that the /b/ people keep mentioning here?

Name: Anonymous 2010-05-30 15:31

>>11
IS THAT MY ANUS

Name: Anonymous 2010-05-30 15:42

>bitfields
Because 8Gb of memory is not enough

Name: Anonymous 2010-05-30 15:54

>>13
Check the other thread where dude is concerned that 16EB will overflow st_size. Clearly 8GB isn't enough, and neither are 64bit words.

Name: Anonymous 2010-05-30 17:30

>>13
That's not a good excuse for being inefficient.

Name: Anonymous 2010-05-30 17:47

sage is 28 bytes, possibly more on the black market.

Name: Anonymous 2010-05-30 22:31

>>15
We all know that depends on the space-time tradeoff you're aiming for, and even that is nuanced in this case.

Don't change these.
Name: Email:
Entire Thread Thread List