How can I use a bit to store a boolean value in C?

Let's talk about algorithms!

Moderator: Board moderators

Post Reply
Learning poster
Posts: 68
Joined: Fri Nov 23, 2001 2:00 am
Location: taiwan

How can I use a bit to store a boolean value in C?

Post by yatsen »

I usually use data type int or char to store a boolean value(0,1).
But sometimes the table is so big.
So if I can use a bit to store a boolean value, it will reduce the memory used.
Please help me, thanks.

A great helper
Posts: 454
Joined: Thu Oct 18, 2001 2:00 am
Location: Hong Kong

Post by .. »

I think this problem should be discussed in "Help on languages" part.........

I use C to write program. When I need to use bit to store boolean, I will use the bit-wise operators ( ^, |, &, <<, >>). Using these operators can let you control the value of a bit in a int or char.
My signature:
  • Please make discussion about the algorithm BRFORE posting source code.
    We can learn much more in discussion than reading source code.
  • I HATE testing account.
  • Don't send me source code for debug.

New poster
Posts: 6
Joined: Thu Jan 16, 2003 6:36 am
Location: Brazil

Boolean in C

Post by 27584NX »

To store boolean values, I use the
[c]_Bool myBooleanVar ;[/c]
The values accepted are 0 or 1. (I think true are false will generate errors)
But this _Bool is not accepted by the GCC compiler (which is the one I think the judge uses), but runs ok on LCCwin32.

Hope I helped you. :P :-?

New poster
Posts: 12
Joined: Fri Jul 19, 2002 6:28 pm
Location: Bangladesh

Post by imranul »

why not use Bit-Field?
Life is like a box of Chocolates,
you never know what you're going to get...

Post Reply

Return to “Algorithms”