Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!


Can you help me with Defining header file and setting bits ??

I need to do the following--
define a bit --
unsigned char data Bits 0x20;

then define the bits:
sbit GoingHigh Bits^0;

then i need a counter byte, i should define it in my header file

unsigned char data Counter 0x30;

Can you help me with this , I am a complete newbie . I keep getting errors. Thank you.


  • LundinLundin Posts: 3,711
    You should do like this:

    [code]#define BIT_MASK 0x20

    unsigned char some_data;

    some_data |= BIT_MASK; /* set bit */
    some_data &= ~BIT_MASK; /* clear bit */

    Which is the same thing as

    some_data = some_data | BIT_MASK;
    some_data = some_data & ~BIT_MASK;

    The above operators are called the "bit-wise" operators. This code works on all C/C++ compilers in the world.

    A poor way to do the same is like this:

    [code]typedef struct
    unsigned int some_data : 1;

    } MyData;

    MyData data;
    data.some_data = 1; /* set bit */
    data.some_data = 0; /* clear bit */[/code]

    This code will compile on all compilers, but it is poor programming. The above is called "bit fields", and bit fields are [italic]very[/italic] poorly defined by the standard. The only allowed type is int, you cannot use them with any other type.

    And you shouldn't use them, but know that they exist. Sadly, they are very commonly used by so-called programmers in embedded systems.

    You shouldn't use them because of the following reasons (advanced topic, beginners may stop reading here :-)):

    - You don't know if the first bit in the struct is msb or lsb.
    - You don't know how the bits are stored in memory.
    - The code will be little/big endian dependant.
    - You don't know whether "int" will be treated as signed or unsigned.
    - You don't know if arrays of bit fields will work or not.
    - Padding bytes are allowed inside bit fields.
    - There is a risk that beginner programmers use other types than int for bit fields, which is undefined behavior in the standard and will cause the code to behave completely randomly.
  • Can you actually explain to me in layment terms -

    what it means by
    define a bit
    [b]unsigned char data Bits 0x20; [/b]
    then define the bits:
    [b]sbit GoingHigh Bits^0;[/b]
    then you need a counter byte, define it in your .h
    [b]unsigned char data Counter 0x30;[/b]

    Going high is just a bit( either 1 or 0)

    and Counter is a byte ( I will be counting from 0 to 50 )

  • LundinLundin Posts: 3,711
    : define a bit
    : [b]unsigned char data Bits 0x20; [/b]

    That's what I did in the first example. Try to dissect that one:

    Since you can only work on byte level on most computers, you need to mask out the bit you are interested in, in this case bit 5, since 0x20 equals bit number 5. You mask out this bit without destroying the other bits. That's when the bitwise OR (|) and AND (&) operators comes in.

    Needless to say, you must understand binary and hexadecimal numbers before starting to learn any form of programming.

    : [b]sbit GoingHigh Bits^0;[/b]

    This means nothing, it is gibberish.

    : then you need a counter byte, define it in your .h
    : [b]unsigned char data Counter 0x30;[/b]

    What do you mean "counter byte" and what has 0x30 to do with that? I don't understand what you are trying to do.

Sign In or Register to comment.