literal binary?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Stephen Mayes

    #1

    literal binary?

    Is there any way to represent a literal numerical value in binary?
    My wish would be something like 0b0011 instead of the hex 0x0F.
    ( if this is even the same. )

    Sometimes I find it's easier to use my finger's than my brain.



  • Artie Gold

    #2
    Re: literal binary?

    Stephen Mayes wrote:[color=blue]
    > Is there any way to represent a literal numerical value in binary?
    > My wish would be something like 0b0011 instead of the hex 0x0F.
    > ( if this is even the same. )[/color]

    There is not. Hexadecimal or octal are your options.[color=blue]
    >
    > Sometimes I find it's easier to use my finger's than my brain.
    >[/color]
    Oh.

    HTH,
    --ag


    --
    Artie Gold -- Austin, Texas
    http://it-matters.blogspot.com (new post 12/5)
    http://www.cafepress.com/goldsays

    Comment

    • Dan Henry

      #3
      Re: literal binary?

      "Stephen Mayes" <delete_stephen @themayeshouse. us> wrote:
      [color=blue]
      >Is there any way to represent a literal numerical value in binary?
      >My wish would be something like 0b0011 instead of the hex 0x0F.
      >( if this is even the same. )
      >
      >Sometimes I find it's easier to use my finger's than my brain.[/color]

      Tom Torfs donated some macros awhile back. His Usenet post is quoted
      below.

      =============== =============== =============== =============== =========
      From: tomto...@villag e.uunet.be (Tom Torfs)
      Newsgroups: comp.lang.c,com p.arch.embedded
      Subject: Binary constant macros
      Date: 26 Feb 2004 07:36:35 -0800
      Organization: http://groups.google.com
      Lines: 66
      Message-ID: <b9d5cc04.04022 60736.60017ce2@ posting.google. com>
      NNTP-Posting-Host: 146.103.254.11
      Content-Type: text/plain; charset=ISO-8859-1
      Content-Transfer-Encoding: 8bit
      X-Trace: posting.google. com 1077809795 7936 127.0.0.1 (26 Feb 2004
      15:36:35 GMT)
      X-Complaints-To: groups-ab...@google.co m
      NNTP-Posting-Date: Thu, 26 Feb 2004 15:36:35 +0000 (UTC)

      Hello All,

      I've been missing the lack of support for binary numeric literals in
      C. To get around it I wrote the following handy macros, which allows
      you to simply write something like:

      whatever = B8(10101010);

      and will translate as:

      whatever = 85;

      (compile-time constant)

      Code below... hopefully it's useful to some of you as well.

      greetings,
      Tom

      /* 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
      */

      greetings,
      Tom
      =============== =============== =============== =============== =========

      --
      Dan Henry

      Comment

      • Jens.Toerring@physik.fu-berlin.de

        #4
        Re: literal binary?

        Stephen Mayes <delete_stephen @themayeshouse. us> wrote:[color=blue]
        > Is there any way to represent a literal numerical value in binary?
        > My wish would be something like 0b0011 instead of the hex 0x0F.
        > ( if this is even the same. )[/color]

        Well, 0x0F would be "0b01111" if such a thing would exist;-)
        But since, at least in my experience, when you deal with single
        bits like that the bits typically have a certain meaning (e.g.
        they are standing for a on/off flags etc., put together into
        a single value) what about defining meaningful names for them
        and then combining them with ORs? Like in e.g.

        #define WRONLY ( 1 << 0 )
        #define RDWR ( 1 << 1 )
        #define CREAT ( 1 << 7 )

        Thereby you could use ( CREAT | RDWR ) instead of 0x82 (or, even
        harder to read, "0b10000010 " as you to wish for) and would have
        the additional ad- vantage of making the code easier to under-
        stand. The program won't get slowed down by that because the
        compiler should be able to replace that by the corresponding
        constant.
        Regards, Jens
        --
        \ Jens Thoms Toerring ___ Jens.Toerring@p hysik.fu-berlin.de
        \______________ ____________ http://www.toerring.de

        Comment

        Working...