Thread: define a boolean type

  1. #1
    Krem
    Guest

    define a boolean type

    Is that anyway we can create a boolean type with 1 bit size?
    I came across a website advertising a C microcontroler compiler
    with BIT and BYTE data types.
    Code:
     bit     boolean;
     byte  message;
    For byte data type, I think it using tydef of char as:
    typedef unsigned char byte;
    But I have no idea to create a bit date type.
    Krem

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    #define bit unsigned char
    #define byte unsigned char

    Either way, you cannot have a variable that is only a single bit in size. You can get one bit variables only as members of a structure or union.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    There are micros, such as the 8051, that have bit addressable memory. A compiler I have used for an 8051 derivative provides non-standard language extensions like a "bit" type to be used with this memory. Since it is non-standard, there is no general C solution for declaring an object that occupies a single bit.

  4. #4
    KingoftheWorld
    Guest

    Re: define a boolean type

    Originally posted by Krem
    Is that anyway we can create a boolean type with 1 bit size?
    I came across a website advertising a C microcontroler compiler
    with BIT and BYTE data types.
    Code:
     bit     boolean;
     byte  message;
    For byte data type, I think it using tydef of char as:
    typedef unsigned char byte;
    But I have no idea to create a bit date type.
    Krem
    Sure, there is a way to create a user defined type
    boolean with 1 bit size.
    Code:
    typedef struct { 
                    unsigned char boolean : 1;
                   } DataType;
    KingoftheWorld
    _________________________________________________
    Love Relationship is like a Software Life Cycle development. It needs to be tried to understand carefully at the early phase and Keep up with enhancing and solid maitainence at the last phase!

  5. #5
    KingoftheWorld
    Guest
    There is a error for my previous solution.
    Here is the final code for bit data type.
    Code:
    typedef struct {
                     unsigned char TRUE  : 1;
                     unsigned char FALSE : 1; 
                   } Boolean;
    KingoftheWorld
    _________________________________________________
    Love Relationship is like a Software Life Cycle development. It needs to be tried to understand carefully at the early phase and Keep up with enhancing and solid maitainence at the last phase!

  6. #6
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    KingOfTheWorld: But that would still use 1 byte of memory.
    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.

  7. #7
    KingoftheWorld
    Guest
    Originally posted by Magos
    KingOfTheWorld: But that would still use 1 byte of memory.
    You right, but boolean value consumes only
    one bit of 1 byte char.
    KingoftheWorld
    _______________________________________________
    Love Relationship is like a Software Life Cycle development. It needs to be tried to understand carefully at the early phase and Keep up with enhancing and solid maitainence at the last phase!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 05-13-2007, 08:55 AM
  2. Please STICKY this- vital to MSVC 6 dev - BASETSD.h
    By VirtualAce in forum Game Programming
    Replies: 11
    Last Post: 03-15-2005, 09:22 AM
  3. build errors migrated from dx9b to dx9c sdk
    By reanimated in forum Game Programming
    Replies: 4
    Last Post: 12-17-2004, 07:35 AM
  4. Directional Keys - Useing in Console
    By RoD in forum C++ Programming
    Replies: 38
    Last Post: 10-06-2002, 04:42 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM