Jump to content

Boolean values


bill

Recommended Posts

Hello,

I need to use few boolean variables in my code.

As i'm a C beginer, i declare an unsigned integer for each of my variables, but i know it's a waste of bits.

Reading mclock.h source code i noticed :

// status of midi clock
typedef union {
  struct {
    unsigned ALL:8;
  };
  struct {
    unsigned RUN:1;
    unsigned PAUSE:1;
    unsigned START_REQ:1;
    unsigned STOP_REQ:1;
    unsigned CONT_REQ:1;
  };
} mclock_state_t;
Is that what i need to declare my boolean variables ? (i need to declare 8 bool vars) is the following code ok ?
//my booleans variables
typedef union {
  struct {
    unsigned ALL:8;
  };
  struct {
    unsigned mybool1:1;
    unsigned mybool2:1;
    unsigned mybool3:1;
    unsigned mybool4:1;
    unsigned mybool5:1;
    unsigned mybool6:1;
    unsigned mybool7:1;
    unsigned mybool8:1;
  };
};

Can someone tell me what is "typedef union" and "struct" basicaly ?

Thanks for your help !

Link to comment
Share on other sites

Hi Bill,

your code is missing a name, but besides that it's okay.

When you are writing a typedef, you create a new type definion. So you don't have only unsigned chars or signed ints anymore but also mytype.

You are using a bitfield as a structure. With 8 bits (maximum allowed value for SDCC!) it needs the same space as an unsigned char and you can store up to 8 boolean values.

A Union is an either-or-thing. Than means, you can either ask ALL bits (and get the 8 bit number; it could also be [tt]unsigned char ALL[/tt] instead of an 8 bit struct) or you are asking the structure and can access every single declared byte. So the data inside the Union remains the same, just the outside form changes.

Hm, I know, it sounds complicated, but actually it's very easy if you take a look at these example code snippets:

Your typedef & declaration should look like that:

// this is optional, but if you're used to booleans, it's a bit nicer:
#define TRUE  1
#define FALSE 0

// typedef
typedef union {
  unsigned char ALL;
  struct {
    unsigned oneBit : 1;
    unsigned theRest: 7;
  }
} myType_t;

// declaration
myType_t myVar;  // got a var 'myVar' of the type 'myType_t'
now here are some example of using these vars:
// usage
myType.ALL = 0x1;  // set all at once
// or
myType.oneBit = TRUE;   // set every bit
myType.theRest = FALSE;

unsigned char c;
c = myType.oneBit;  // c is 1
c = myType.ALL;  // c is 1
or another example:
myType.oneBit = 0;
myType.theRest = 1;

c = myType.ALL;  // ALL should be 2, cause the second bit is 1 (the first, third - seventh are all 0)
                         // so you got: 0 0 0 0 0 1 0 => 0x2

myType.oneBit = 1;
c = myType.ALL;  // ALL is now 3 => 0 0 0 0 0 1 1 => 0x3

Cheers!

Michael

Link to comment
Share on other sites

  • 3 weeks later...

also dann ist dieses unsigned nicht nur ein bool-wert, sondern ein bitfeld, wenn man das durch ein ":" und einer Zahl dahinter deklariert?

geht sowas in C++ auch? hab ich noch nie gesehn...

geht sowas auch ohne eine Struktur oder Union?

ich glaub da kam bei mir eine fehlermeldung:

unsigned test:2;

Link to comment
Share on other sites

no, [tt]unsigned[/tt] is neither a bool-value nor a bitfield. it just sais that it's no negative number.

For example, [tt]unsigned char[/tt] is a basic type (typedefinition) and means that the number (char) is not negative. (mit / ohne Vorzeichen)

signed char: -127 to 127

unsigned char: 0 to 255

char: range depending on compiler, normally -127 to 127

Here's a link to a very good C book in german:

http://www.galileocomputing.de/openbook/c_von_a_bis_z/

and btw -

we got an extra board for native german language requests:

http://www.midibox.org/forum/index.php?board=18.0

Regards,

Michael

Link to comment
Share on other sites

thx, but this infos is well-knowed.

I thought unsigned must be a datatype (or a flag), because it's normal to do declarations like this (maybe in c++):

int int a;  // (int * int)

... so i thought i can use unsigned as a one bit datatyp (flag) too:

unsigned a;  // there is no compiling error!

..but maybe it's same like unsigned char a;, because the smallest alloced datatyp is an byte in memory, right?

So i only have to use "bitfields" in structures, i'm right?

but i have to use always 8 BIT, never less and all unused i've to declare without a name.., correct?

Can i use 16 BIT Bitfield too in a struct?

Link to comment
Share on other sites

  • 1 year later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...