This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

How to read a Bit from a Byte ?

Respectable colleagues,

how to read a value of a BIT from value of a BYTE variable in Keil C ?

For example:
X=(Byte1.Bit7*4)+(Byte1.Bit6*2)

Thanks in advance.

With best regards,

Dragan Kujovic

Parents
  • You need to create your variable in BDATA zone and defined its bits :

    char bdata BYTE;

    sbit Bit0 = BYTE ^ 0;
    sbit Bit1 = BYTE ^ 1;
    sbit Bit2 = BYTE ^ 2;
    sbit Bit3 = BYTE ^ 3;
    sbit Bit4 = BYTE ^ 4;
    sbit Bit5 = BYTE ^ 5;
    sbit Bit6 = BYTE ^ 6;
    sbit Bit7 = BYTE ^ 7;

    Then you'll be able to access each bit individually.

    nb:you need to use a type cast (int) to multiply a bit by a number

    int X=(int)Bit7*4 + (int)Bit6*2;

    Gilles

Reply
  • You need to create your variable in BDATA zone and defined its bits :

    char bdata BYTE;

    sbit Bit0 = BYTE ^ 0;
    sbit Bit1 = BYTE ^ 1;
    sbit Bit2 = BYTE ^ 2;
    sbit Bit3 = BYTE ^ 3;
    sbit Bit4 = BYTE ^ 4;
    sbit Bit5 = BYTE ^ 5;
    sbit Bit6 = BYTE ^ 6;
    sbit Bit7 = BYTE ^ 7;

    Then you'll be able to access each bit individually.

    nb:you need to use a type cast (int) to multiply a bit by a number

    int X=(int)Bit7*4 + (int)Bit6*2;

    Gilles

Children
No data