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 represent a binary number in C51?

Hi,
How to represent a binary number in C51? I wrote, for example, 11110000b but the C51 don't agree!
Please, answer me. Thank you.
Hoang Kong Kong.
Ha Noi-Viet Nam.

Parents
  • Well, may I suggest a little hint? (=

    You see, some C compilers have binary notation (like, 0B, 0Bx etc). But pure ANSI C does not. Anyway, if you set and read about that language so have found one magic word called "MACRO". Okay, it's not easy to create such macro on-the-fly if you are not familar with C and binary/octal/decimal formats but... Here you have got it:

    #define bin(a) ((( (a/10000000*128) + \
    (((a/1000000)&1)*64) + \
    (((a/100000)&1)*32) + \
    (((a/10000)&1)*16) + \
    (((a/1000)&1)*8) + \
    (((a/100)&1)*4) + \
    (((a/10)&1)*2) + \
    (a&1)) * (a/10000000)) + \
    (( ((a/262144)*64) + \
    (((a/32768)&1)*32) + \
    (((a/4096)&1)*16) + \
    (((a/512)&1)*8) + \
    (((a/64)&1)*4) + \
    (((a/8)&1)*2) + \
    (a&1)) * (1-(a/10000000))))

    Well, it's hard to imagine what does it do (=
    But just put these lines above your code and use something like:

    unsigned char b1, b2;

    b1 = bin(10101010); // 0xAA
    b2 = bin(01010101); // 0x55

    Note: I do not know, does it work in Keil C or not; at least, in pure ANSI C it does work correctly.

    Good days!

Reply
  • Well, may I suggest a little hint? (=

    You see, some C compilers have binary notation (like, 0B, 0Bx etc). But pure ANSI C does not. Anyway, if you set and read about that language so have found one magic word called "MACRO". Okay, it's not easy to create such macro on-the-fly if you are not familar with C and binary/octal/decimal formats but... Here you have got it:

    #define bin(a) ((( (a/10000000*128) + \
    (((a/1000000)&1)*64) + \
    (((a/100000)&1)*32) + \
    (((a/10000)&1)*16) + \
    (((a/1000)&1)*8) + \
    (((a/100)&1)*4) + \
    (((a/10)&1)*2) + \
    (a&1)) * (a/10000000)) + \
    (( ((a/262144)*64) + \
    (((a/32768)&1)*32) + \
    (((a/4096)&1)*16) + \
    (((a/512)&1)*8) + \
    (((a/64)&1)*4) + \
    (((a/8)&1)*2) + \
    (a&1)) * (1-(a/10000000))))

    Well, it's hard to imagine what does it do (=
    But just put these lines above your code and use something like:

    unsigned char b1, b2;

    b1 = bin(10101010); // 0xAA
    b2 = bin(01010101); // 0x55

    Note: I do not know, does it work in Keil C or not; at least, in pure ANSI C it does work correctly.

    Good days!

Children
No data