using binary numbers in c

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Kelvin@!!!

    using binary numbers in c

    Hi everyone:
    when we wanna use hex numbers in C, we usually write something like:
    int hex_num = 0x12F9;

    but how can I declare a binary number in a similar way by putting some
    leading words to tell the complier this is a binary number???
    similarly, in printf, we have %d for an decimal number %x and %o for hex and
    octal numbers... how about binary numbers??

    Thank you very much...
    --
    { Kelvin@!!! }


  • Leor Zolman

    #2
    Re: using binary numbers in c

    On Thu, 06 May 2004 01:18:56 GMT, "Kelvin@!!! " <kelvin604@shaw .ca.ca>
    wrote:
    [color=blue]
    >Hi everyone:
    >when we wanna use hex numbers in C, we usually write something like:
    >int hex_num = 0x12F9;
    >
    >but how can I declare a binary number in a similar way by putting some
    >leading words to tell the complier this is a binary number???
    >similarly, in printf, we have %d for an decimal number %x and %o for hex and
    >octal numbers... how about binary numbers??
    >
    >Thank you very much...[/color]

    Here's something I picked up from, I believe, this very newsgroup not long
    back:

    ------------- macros.h: ---------------------------------

    /*
    macros.h:
    Binary constant generator macro
    By Tom Torfs - donated to the public domain
    */

    /* All macro's evaluate to compile-time constants */

    /* *** helper macros *** */

    /* turn a numeric literal into a hex constant
    (avoids problems with leading zeroes)
    8-bit constants max value 0x11111111, always fits in unsigned long
    */
    #define HEX__(n) 0x##n##LU

    /* 8-bit conversion function */
    #define B8__(x) ((x&0x0000000FL U)?1:0) \
    +((x&0x000000F0 LU)?2:0) \
    +((x&0x00000F00 LU)?4:0) \
    +((x&0x0000F000 LU)?8:0) \
    +((x&0x000F0000 LU)?16:0) \
    +((x&0x00F00000 LU)?32:0) \
    +((x&0x0F000000 LU)?64:0) \
    +((x&0xF0000000 LU)?128:0)

    /* *** user macros *** */

    /* for upto 8-bit binary constants */
    #define B8(d) ((unsigned char)B8__(HEX__ (d)))

    /* for upto 16-bit binary constants, MSB first */
    #define B16(dmsb,dlsb) (((unsigned short)B8(dmsb)< <8) \
    + B8(dlsb))

    /* for upto 32-bit binary constants, MSB first */
    #define B32(dmsb,db2,db 3,dlsb) (((unsigned long)B8(dmsb)<< 24) \
    + ((unsigned long)B8(db2)<<1 6) \
    + ((unsigned long)B8(db3)<<8 ) \
    + B8(dlsb))

    /* Sample usage:
    B8(01010101) = 85
    B16(10101010,01 010101) = 43605
    B32(10000000,11 111111,10101010 ,01010101) = 2164238933
    */

    ------------ test.c ---------------------

    #include <stdio.h>
    #include "macros.h"

    int main()
    {
    int i = B8(1010);
    int j = B8(10000000);

    printf("i = %d, j = %d\n", i, j);
    return 0;
    }


    HTH,
    -leor


    --
    Leor Zolman --- BD Software --- www.bdsoft.com
    On-Site Training in C/C++, Java, Perl and Unix
    C++ users: download BD Software's free STL Error Message Decryptor at:
    An STL Error Decryptor for C++ by Leor Zolman of BD Software - available to download here

    Comment

    • Mike Wahler

      #3
      Re: using binary numbers in c


      "Kelvin@!!! " <kelvin604@shaw .ca.ca> wrote in message
      news:4ogmc.3842 22$Ig.25579@pd7 tw2no...[color=blue]
      > Hi everyone:
      > when we wanna use hex numbers in C, we usually write something like:
      > int hex_num = 0x12F9;
      >
      > but how can I declare a binary number in a similar way by putting some
      > leading words to tell the complier this is a binary number???[/color]

      First let me dispel what I think is an erroneous notion you have.
      All numbers are stored as 'binary'. When you talk about e.g.
      'decimal', 'binary', 'hex', etc., you're talking about the
      *textual* representation of a number, e.g. using digits 0-9,
      0 and 1, 0 - F, etc.

      That said, no, C has no syntax for expressing a binary pattern in source
      code. Hex is as close (and imo more 'convenient') as it gets.
      [color=blue]
      > similarly, in printf, we have %d for an decimal number %x and %o for hex[/color]
      and[color=blue]
      > octal numbers... how about binary numbers??[/color]

      Nope, printf() doesn't have a type specifier for "zero and one"
      representation. But it's trivial to write a function
      to produce a string of zero and one characters from an integer,
      then use %s with its output.

      Hints:

      x % 2;
      x /= 2;

      A function could also be written to input a string containing
      zeros and ones and convert it to a numeric type.

      -Mike


      Comment

      • Kelvin@!!!

        #4
        Re: using binary numbers in c


        "Leor Zolman" <leor@bdsoft.co m> wrote in message
        news:d47j901nbi gpqv23rp2h71e3e o24ligt1n@4ax.c om...[color=blue]
        > On Thu, 06 May 2004 01:18:56 GMT, "Kelvin@!!! " <kelvin604@shaw .ca.ca>
        > wrote:
        >[color=green]
        > >Hi everyone:
        > >when we wanna use hex numbers in C, we usually write something like:
        > >int hex_num = 0x12F9;
        > >
        > >but how can I declare a binary number in a similar way by putting some
        > >leading words to tell the complier this is a binary number???
        > >similarly, in printf, we have %d for an decimal number %x and %o for hex[/color][/color]
        and[color=blue][color=green]
        > >octal numbers... how about binary numbers??
        > >
        > >Thank you very much...[/color]
        >
        > Here's something I picked up from, I believe, this very newsgroup not long
        > back:
        >
        > ------------- macros.h: ---------------------------------
        >
        > /*
        > macros.h:
        > Binary constant generator macro
        > By Tom Torfs - donated to the public domain
        > */
        >
        > /* All macro's evaluate to compile-time constants */
        >
        > /* *** helper macros *** */
        >
        > /* turn a numeric literal into a hex constant
        > (avoids problems with leading zeroes)
        > 8-bit constants max value 0x11111111, always fits in unsigned long
        > */
        > #define HEX__(n) 0x##n##LU
        >
        > /* 8-bit conversion function */
        > #define B8__(x) ((x&0x0000000FL U)?1:0) \
        > +((x&0x000000F0 LU)?2:0) \
        > +((x&0x00000F00 LU)?4:0) \
        > +((x&0x0000F000 LU)?8:0) \
        > +((x&0x000F0000 LU)?16:0) \
        > +((x&0x00F00000 LU)?32:0) \
        > +((x&0x0F000000 LU)?64:0) \
        > +((x&0xF0000000 LU)?128:0)
        >
        > /* *** user macros *** */
        >
        > /* for upto 8-bit binary constants */
        > #define B8(d) ((unsigned char)B8__(HEX__ (d)))
        >
        > /* for upto 16-bit binary constants, MSB first */
        > #define B16(dmsb,dlsb) (((unsigned short)B8(dmsb)< <8) \
        > + B8(dlsb))
        >
        > /* for upto 32-bit binary constants, MSB first */
        > #define B32(dmsb,db2,db 3,dlsb) (((unsigned long)B8(dmsb)<< 24) \
        > + ((unsigned long)B8(db2)<<1 6) \
        > + ((unsigned long)B8(db3)<<8 ) \
        > + B8(dlsb))
        >
        > /* Sample usage:
        > B8(01010101) = 85
        > B16(10101010,01 010101) = 43605
        > B32(10000000,11 111111,10101010 ,01010101) = 2164238933
        > */
        >
        > ------------ test.c ---------------------
        >
        > #include <stdio.h>
        > #include "macros.h"
        >
        > int main()
        > {
        > int i = B8(1010);
        > int j = B8(10000000);
        >
        > printf("i = %d, j = %d\n", i, j);
        > return 0;
        > }
        >
        >
        > HTH,
        > -leor
        >
        >
        > --
        > Leor Zolman --- BD Software --- www.bdsoft.com
        > On-Site Training in C/C++, Java, Perl and Unix
        > C++ users: download BD Software's free STL Error Message Decryptor at:
        > www.bdsoft.com/tools/stlfilt.html[/color]

        thank you for the code...
        but ...[color=blue]
        > #define HEX__(n) 0x##n##LU[/color]

        what does the # stands for here???

        --
        { Kelvin@!!! }


        Comment

        • Joona I Palaste

          #5
          Re: using binary numbers in c

          Kelvin@!!! <kelvin604@shaw .ca.ca> scribbled the following:[color=blue]
          > "Leor Zolman" <leor@bdsoft.co m> wrote in message
          > news:d47j901nbi gpqv23rp2h71e3e o24ligt1n@4ax.c om...[color=green]
          >> Here's something I picked up from, I believe, this very newsgroup not long
          >> back:[/color][/color]

          (snip)
          [color=blue]
          > thank you for the code...
          > but ...[color=green]
          >> #define HEX__(n) 0x##n##LU[/color][/color]
          [color=blue]
          > what does the # stands for here???[/color]

          Assuming you know what #define means and are asking about the ##:
          It's a preprocessor operator that "glues" two preprocessing tokens into
          one C token. Here it's used twice, gluing three preprocessing tokens
          together. The first is 0x, the second is whatever n gets replaced with,
          and the third is LU. For example HEX__(0) would be expanded to a glued
          together token 0x0LU. The C compiler itself treats this as a single
          token and not as three tokens after each other.

          --
          /-- Joona Palaste (palaste@cc.hel sinki.fi) ------------- Finland --------\
          \-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
          "It sure is cool having money and chicks."
          - Beavis and Butt-head

          Comment

          • Mabden

            #6
            Re: using binary numbers in c

            "Kelvin@!!! " <kelvin604@shaw .ca.ca> wrote in message
            news:4ogmc.3842 22$Ig.25579@pd7 tw2no...[color=blue]
            > Hi everyone:
            > when we wanna use hex numbers in C, we usually write something like:
            > int hex_num = 0x12F9;
            >
            > but how can I declare a binary number in a similar way by putting some
            > leading words to tell the complier this is a binary number???
            > similarly, in printf, we have %d for an decimal number %x and %o for hex[/color]
            and[color=blue]
            > octal numbers... how about binary numbers??
            >[/color]

            There isn't a way to do that, as the compiler does know from binary.

            OTOH, you could write a pre-processor that does that, if you wish. It would
            take your new symbol, say 0b11110000 and turn it into 0xF0, then pass it
            into the compiler.

            For printf(), you'd have to write a function to change hex to a binary
            string, I surmise. It would have to be at run-time since the variables, are.

            --
            Mabden


            Comment

            • Kieran Simkin

              #7
              Re: using binary numbers in c



              --


              ~Kieran Simkin
              Digital Crocus


              "Kelvin@!!! " <kelvin604@shaw .ca.ca> wrote in message
              news:4ogmc.3842 22$Ig.25579@pd7 tw2no...[color=blue]
              > Hi everyone:
              > when we wanna use hex numbers in C, we usually write something like:
              > int hex_num = 0x12F9;
              >
              > but how can I declare a binary number in a similar way by putting some
              > leading words to tell the complier this is a binary number???
              > similarly, in printf, we have %d for an decimal number %x and %o for hex[/color]
              and[color=blue]
              > octal numbers... how about binary numbers??
              >
              > Thank you very much...
              > --
              > { Kelvin@!!! }
              >
              >[/color]


              Comment

              Working...