I have an unsigned char (8-bits wide, naturally) that contains 8 flag bits.

Example:

unsigned char flag1:1, 
            flag2:1, 
            flag3:1, 
            flag4:1,
            flag5:1, 
            flag6:1,
            flag7:1, 
            flag8:1;

I would like to take this unsigned char and convert it into a bit array where each bit represents a flag. Pretty much like representing that unsigned char in its true binary form.


Similar example to what im looking for:

The character 'a' is 61 in hex which is 0110 0001 in binary. If I were to pass the char 'a' into this function, it would return to me a bit pointer (or an 8 bit array) starting with the first address of it's binary form.

There should be an easy way of doing this. No?

Recommended Answers

All 7 Replies

Was hoping there would be :)

Ok, so what would be the easiest/fastest way to get those flags?


I was thinking to use a struct like:

struct test
{
	unsigned char	a:1,
					b:1,
					c:1,
					d:1,
					e:1,
					f:1,
					g:1,
					h:1;
};

but I'm still kinda new at C so im still learning this pointer stuff ( easy actually when you think of it ).

Maybe I can do something like this:

(I know this code is probably horrifying and wont work, but maybe you can fix it for me so i can learn)

int main(int argc, char **argv)
{
	struct test *t = (struct test *)'a';
	printf("%s\n", t);               //should print out:   a

        printf("%d\n", t->a)        //should print out the 0 bit            (FAIL exit status)
        printf("%d\n", t->b)        //should print out the 1 bit
        printf("%d\n", t->c)        //should print out the 2 bit
        // .....etc.....
}

Actually, i get a seg fault....... i've been getting those a lot in c... very annoying...

a bit would act more like a boolean right? not a char or int cause those are multiple bits... I would have to do:

#define STAT(x) ((x)) ? 1 : 0

so theoretically, printf("%d\n", STAT(t->a)) should now print out a 0 or 1 right?

nope... still seg fault... any help?

Your pointer is initialized in a screwed-up way. I think you've got bigger fish to fry first. What do you think you're trying to do with your 'bit array' that isn't already done with masks?

Your pointer is initialized in a screwed-up way. I think you've got bigger fish to fry first. What do you think you're trying to do with your 'bit array' that isn't already done with masks?

I totally agree with Dave Sinkula's response. Bit masking would be far easier especially if you encapsulate the logic into a function.

If you really want to go down your original path, then try the following code snippet out. It uses a union (not sure if you understand unions). But again, you're better of just using plain ol' bit masking to achieve your goal.

But as Salem pointed out earlier, there is no such thing as an array of bits, so the code below doesn't give you much except for "reading" what those bits in your structure are after loading up character (it really is overkill).

#include <stdio.h>

int main(void) {

    struct bits {
        unsigned char b8 :1,
                      b7 :1,
                      b6 :1,
                      b5 :1,
                      b4 :1,
                      b3 :1,
                      b2 :1,
                      b1 :1;
    };

    union uchar {
        struct bits chbits;
        unsigned char ch;
    } mychar;

    printf("Enter a character: ");
    scanf("%c", &mychar.ch);

    printf("Binary representation of the character %c : %d%d%d%d%d%d%d%d\n", mychar.ch,
            mychar.chbits.b1, mychar.chbits.b2, mychar.chbits.b3,
            mychar.chbits.b4, mychar.chbits.b5, mychar.chbits.b6,
            mychar.chbits.b7, mychar.chbits.b8);

    return 0;
}

Cheers,
JD

you're better of just using plain ol' bit masking to achieve your goal.

I'm looking for the best way to extract each bit from an 8-bit unsigned char.

My goggling attempts for "bit masking in c" didn't really churn up much results. Can you give me a quick example of how I would extract a bit from an unsigned character using bit masking?

If I understand correctly, simply masking a few bits and changing them would just change the unsigned character to a different char. How would I extract each bit easily like this?

OK! Got it!

int extract(char data, int i) {
    return (data >> i) & 0x01;
}

int main(void)
{
	int i;
	for (i=0; i<8; i++)
		printf("%d ", extract('a', i));
	
}

Thank you. I didn't know about shifting and masking in C. This is very useful. Much appreciated!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.