print binary representation

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Joe Wright

    #16
    Re: print binary representation

    Carramba wrote:
    Harald van Dijk wrote:
    >Carramba wrote:
    >>Hi!
    >>>
    >>How can I output value of char or int in binary form with printf(); ?
    >>>
    >>thanx in advance
    >>
    >There is no standard format specifier for binary form. You will have
    >to do the conversion manually, testing each bit from highest to
    >lowest, printing '0' if it's not set, and '1' if it is.
    >>
    thanx, maybe you have so suggestion or link for further reading on how
    to do it?
    Think about it!

    void bits(unsigned char b, int n) {
    for (--n; n >= 0; --n)
    putchar((b & 1 << n) ? '1' : '0');
    putchar(' ');
    }

    Now if you call it..

    bits(195, 8);

    ...you'll get '11000011 ' on the stdout stream.

    --
    Joe Wright
    "Everything should be made as simple as possible, but not simpler."
    --- Albert Einstein ---

    Comment

    • Barry Schwarz

      #17
      Re: print binary representation

      On 24 Mar 2007 04:42:24 -0700, "Harald van D?k" <truedfx@gmail. com>
      wrote:
      >Carramba wrote:
      >Harald van D?k wrote:
      Carramba wrote:
      >Hi!
      >>
      >How can I output value of char or int in binary form with printf(); ?
      >>
      >thanx in advance
      >
      There is no standard format specifier for binary form. You will have
      to do the conversion manually, testing each bit from highest to
      lowest, printing '0' if it's not set, and '1' if it is.
      >
      >thanx, maybe you have so suggestion or link for further reading on how
      >to do it?
      >
      >Others have given code already, but here's mine anyway:
      >
      >#include <limits.h>
      >#include <stdio.h>
      >
      >void print_char_bina ry(char val)
      >{
      char mask;
      >
      if(CHAR_MIN < 0)
      {
      if(val < 0
      || val == 0 && val & CHAR_MAX)
      When will the expression following the && evaluate to 1? Is it
      something to do with ones complement or signed magnitude
      representations ?
      putchar('1');
      else
      putchar('0');
      }
      >
      for(mask = (CHAR_MAX >1) + 1; mask != 0; mask >>= 1)
      Is it a requirement that (CHAR_MAX>>1)+1 be a power of 2? It is a
      requirement for UCHAR_MAX but what if char is signed? (If CHAR_BIT is
      9, could SCHAR_MAX and CHAR_MAX be 173?)
      if(val & mask)
      putchar('1');
      else
      putchar('0');
      >}
      >
      >void print_int_binar y(int val)
      >{
      int mask;
      >
      if(val < 0
      || val == 0 && val & INT_MAX)
      putchar('1');
      else
      putchar('0');
      >
      for(mask = (INT_MAX >1) + 1; mask != 0; mask >>= 1)
      There does not appear to be a similar requirement for INT_MAX either.
      if(val & mask)
      putchar('1');
      else
      putchar('0');
      >}

      Remove del for email

      Comment

      • =?utf-8?B?SGFyYWxkIHZhbiBExLNr?=

        #18
        Re: print binary representation

        Barry Schwarz wrote:
        On 24 Mar 2007 04:42:24 -0700, "Harald van D?k" <truedfx@gmail. com>
        wrote:
        >
        Carramba wrote:
        Harald van D?k wrote:
        Carramba wrote:
        Hi!
        >
        How can I output value of char or int in binary form with printf(); ?
        >
        thanx in advance

        There is no standard format specifier for binary form. You will have
        to do the conversion manually, testing each bit from highest to
        lowest, printing '0' if it's not set, and '1' if it is.

        thanx, maybe you have so suggestion or link for further reading on how
        to do it?
        Others have given code already, but here's mine anyway:

        #include <limits.h>
        #include <stdio.h>

        void print_char_bina ry(char val)
        {
        char mask;

        if(CHAR_MIN < 0)
        {
        if(val < 0
        || val == 0 && val & CHAR_MAX)
        >
        When will the expression following the && evaluate to 1? Is it
        something to do with ones complement or signed magnitude
        representations ?
        It accounts for ones' complement, where all bits 1 is a possible
        representation of 0.

        It does not account for sign and magnitude, where all value bits 0 and
        sign bit 1 is a representation of 0. This will be printed as all bits
        zero, which is a different representation of the same value.
        putchar('1');
        else
        putchar('0');
        }

        for(mask = (CHAR_MAX >1) + 1; mask != 0; mask >>= 1)
        >
        Is it a requirement that (CHAR_MAX>>1)+1 be a power of 2? It is a
        requirement for UCHAR_MAX but what if char is signed? (If CHAR_BIT is
        9, could SCHAR_MAX and CHAR_MAX be 173?)
        [ And a similar comment for INT_MAX snipped ]

        The only allowed representation systems for signed integer types are
        two's complement, ones' complement, and sign and magnitude. All three
        have the maximum value as a power of two minus one. (IIRC, this is new
        in C99, but it was added because there were no other systems even
        though C90 allowed it.)

        Comment

        • Malcolm McLean

          #19
          Re: print binary representation

          "Harald van Dijk" <truedfx@gmail. comwrote in message
          >
          The only allowed representation systems for signed integer types are
          two's complement, ones' complement, and sign and magnitude. All three
          have the maximum value as a power of two minus one. (IIRC, this is new
          in C99, but it was added because there were no other systems even
          though C90 allowed it.)
          >
          That's typical committee thinking. No engineer is going to devise a new
          method of representing integers for the fun of it, but because there is some
          technical advantage or requirement. At which point the standard becomes a
          dead letter. If the super-whizzy-fibby machine needs Fibonacci
          representation for its quantum coherence modulator unit, the either C can't
          be used on such a machine or the rule will change. So it is a completely
          pointless regulation.

          --
          Free games and programming goodies.


          Comment

          • Barry Schwarz

            #20
            Re: print binary representation

            On 24 Mar 2007 10:51:58 -0700, "Harald van D?k" <truedfx@gmail. com>
            wrote:
            >Barry Schwarz wrote:
            >On 24 Mar 2007 04:42:24 -0700, "Harald van D?k" <truedfx@gmail. com>
            >wrote:
            >>
            >Carramba wrote:
            >Harald van D?k wrote:
            Carramba wrote:
            >Hi!
            >>
            >How can I output value of char or int in binary form with printf(); ?
            >>
            >thanx in advance
            >
            There is no standard format specifier for binary form. You will have
            to do the conversion manually, testing each bit from highest to
            lowest, printing '0' if it's not set, and '1' if it is.
            >
            >thanx, maybe you have so suggestion or link for further reading on how
            >to do it?
            >
            >Others have given code already, but here's mine anyway:
            >
            >#include <limits.h>
            >#include <stdio.h>
            >
            >void print_char_bina ry(char val)
            >{
            char mask;
            >
            if(CHAR_MIN < 0)
            {
            if(val < 0
            || val == 0 && val & CHAR_MAX)
            >>
            >When will the expression following the && evaluate to 1? Is it
            >something to do with ones complement or signed magnitude
            >representation s?
            >
            >It accounts for ones' complement, where all bits 1 is a possible
            >representati on of 0.
            >
            >It does not account for sign and magnitude, where all value bits 0 and
            >sign bit 1 is a representation of 0. This will be printed as all bits
            >zero, which is a different representation of the same value.
            >
            putchar('1');
            else
            putchar('0');
            }
            >
            for(mask = (CHAR_MAX >1) + 1; mask != 0; mask >>= 1)
            >>
            >Is it a requirement that (CHAR_MAX>>1)+1 be a power of 2? It is a
            >requirement for UCHAR_MAX but what if char is signed? (If CHAR_BIT is
            >9, could SCHAR_MAX and CHAR_MAX be 173?)
            >
            >[ And a similar comment for INT_MAX snipped ]
            >
            >The only allowed representation systems for signed integer types are
            >two's complement, ones' complement, and sign and magnitude. All three
            >have the maximum value as a power of two minus one. (IIRC, this is new
            >in C99, but it was added because there were no other systems even
            >though C90 allowed it.)
            n1124 says that UCHAR_MAX must be equal to 2^CHAR_BIT-1 which I
            mentioned in my question. For SCHAR_MAX, there is no such
            requirement. It is required to be at least (minimum value) 127 which
            is 2^7-1 but for larger values of CHAR_BIT there is no additional
            restriction. Again, if CHAR_BIT is 9, could SCHAR_MAX and CHAR_MAX be
            173?


            Remove del for email

            Comment

            • =?utf-8?B?SGFyYWxkIHZhbiBExLNr?=

              #21
              Re: print binary representation

              Barry Schwarz wrote:
              On 24 Mar 2007 10:51:58 -0700, "Harald van D?k" <truedfx@gmail. com>
              wrote:
              The only allowed representation systems for signed integer types are
              two's complement, ones' complement, and sign and magnitude. All three
              have the maximum value as a power of two minus one. (IIRC, this is new
              in C99, but it was added because there were no other systems even
              though C90 allowed it.)
              >
              n1124 says that UCHAR_MAX must be equal to 2^CHAR_BIT-1 which I
              mentioned in my question. For SCHAR_MAX, there is no such
              requirement. It is required to be at least (minimum value) 127 which
              is 2^7-1 but for larger values of CHAR_BIT there is no additional
              restriction. Again, if CHAR_BIT is 9, could SCHAR_MAX and CHAR_MAX be
              173?
              Again, no. The only allowed representation systems for signed integers
              are those three. If SCHAR_MAX is 173 (or if INT_MAX is 99999), then
              the representation system cannot be one of those three, so the
              implementation would violate 6.2.6.2p2. The fact that there is an
              explicit statement that UCHAR_MAX must equal 2^CHAR_BIT - 1 doesn't
              seem relevant to me, because unsigned char is already a special case
              (as the only integer type that may not contain trap representations or
              padding bits).

              Comment

              • SM Ryan

                #22
                Re: print binary representation

                Carramba <user@example.n etwrote:
                # Hi!
                #
                # How can I output value of char or int in binary form with printf(); ?

                You might sprintf with %x and then just map the hex digits
                to four character strings.

                --
                SM Ryan http://www.rawbw.com/~wyrmwif/
                Quit killing people. That's high profile.

                Comment

                • Barry Schwarz

                  #23
                  Re: print binary representation

                  On 24 Mar 2007 14:39:23 -0700, "Harald van D?k" <truedfx@gmail. com>
                  wrote:
                  >Barry Schwarz wrote:
                  >On 24 Mar 2007 10:51:58 -0700, "Harald van D?k" <truedfx@gmail. com>
                  >wrote:
                  >The only allowed representation systems for signed integer types are
                  >two's complement, ones' complement, and sign and magnitude. All three
                  >have the maximum value as a power of two minus one. (IIRC, this is new
                  >in C99, but it was added because there were no other systems even
                  >though C90 allowed it.)
                  >>
                  >n1124 says that UCHAR_MAX must be equal to 2^CHAR_BIT-1 which I
                  >mentioned in my question. For SCHAR_MAX, there is no such
                  >requirement. It is required to be at least (minimum value) 127 which
                  >is 2^7-1 but for larger values of CHAR_BIT there is no additional
                  >restriction. Again, if CHAR_BIT is 9, could SCHAR_MAX and CHAR_MAX be
                  >173?
                  >
                  >Again, no. The only allowed representation systems for signed integers
                  >are those three. If SCHAR_MAX is 173 (or if INT_MAX is 99999), then
                  >the representation system cannot be one of those three, so the
                  >implementati on would violate 6.2.6.2p2. The fact that there is an
                  >explicit statement that UCHAR_MAX must equal 2^CHAR_BIT - 1 doesn't
                  >seem relevant to me, because unsigned char is already a special case
                  >(as the only integer type that may not contain trap representations or
                  >padding bits).
                  I can find no requirement that every possible bit combination must be
                  a valid value. The fact that a signed 9-bit char can support a value
                  larger than 173 in any of the three allowed representations doesn't
                  mean it has to.


                  Remove del for email

                  Comment

                  • CBFalconer

                    #24
                    Re: print binary representation

                    Carramba wrote:
                    >
                    How can I output value of char or int in binary form with printf(); ?
                    #include <stdio.h>
                    #include <stdlib.h>

                    static void binprt(long i) {
                    if (i / 2) binprt(i / 2);
                    putchar('0' + i % 2);
                    } /* binprt */

                    /* ----------------- */

                    int main(int argc, char* *argv) {
                    long x;

                    if ((argc != 2) || (1 != sscanf(argv[1], "%ld", &x))) {
                    fprintf(stderr, "Usage: binprt value\n");
                    exit(EXIT_FAILU RE);
                    }
                    binprt(x);
                    putchar('\n');
                    return 0;
                    }

                    --
                    Chuck F (cbfalconer at maineline dot net)
                    Available for consulting/temporary embedded and systems.
                    <http://cbfalconer.home .att.net>



                    --
                    Posted via a free Usenet account from http://www.teranews.com

                    Comment

                    • pete

                      #25
                      Re: print binary representation

                      Barry Schwarz wrote:
                      I can find no requirement that every possible bit combination must be
                      a valid value. The fact that a signed 9-bit char can support a value
                      larger than 173 in any of the three allowed representations doesn't
                      mean it has to.
                      There are not three allowable representations for positive values
                      of signed types.

                      N869
                      6.2.6.2 Integer types

                      [#2] For signed integer types, the bits of the object
                      representation shall be divided into three groups: value
                      bits, padding bits, and the sign bit. There need not be any
                      padding bits; there shall be exactly one sign bit. Each bit
                      that is a value bit shall have the same value as the same
                      bit in the object representation of the corresponding
                      unsigned type (if there are M value bits in the signed type
                      and N in the unsigned type, then M<=N). If the sign bit is
                      zero, it shall not affect the resulting value.

                      The only description of value bits,
                      is in the description of the representation of unsigned types,
                      so, value bits in the signed type
                      should behave the same way for positive values.

                      --
                      pete

                      Comment

                      • =?utf-8?B?SGFyYWxkIHZhbiBExLNr?=

                        #26
                        Re: print binary representation

                        Barry Schwarz wrote:
                        On 24 Mar 2007 14:39:23 -0700, "Harald van D?k" <truedfx@gmail. com>
                        wrote:
                        Barry Schwarz wrote:
                        On 24 Mar 2007 10:51:58 -0700, "Harald van D?k" <truedfx@gmail. com>
                        wrote:
                        The only allowed representation systems for signed integer types are
                        two's complement, ones' complement, and sign and magnitude. All three
                        have the maximum value as a power of two minus one. (IIRC, this is new
                        in C99, but it was added because there were no other systems even
                        though C90 allowed it.)
                        >
                        n1124 says that UCHAR_MAX must be equal to 2^CHAR_BIT-1 which I
                        mentioned in my question. For SCHAR_MAX, there is no such
                        requirement. It is required to be at least (minimum value) 127 which
                        is 2^7-1 but for larger values of CHAR_BIT there is no additional
                        restriction. Again, if CHAR_BIT is 9, could SCHAR_MAX and CHAR_MAX be
                        173?
                        Again, no. The only allowed representation systems for signed integers
                        are those three. If SCHAR_MAX is 173 (or if INT_MAX is 99999), then
                        the representation system cannot be one of those three, so the
                        implementation would violate 6.2.6.2p2. The fact that there is an
                        explicit statement that UCHAR_MAX must equal 2^CHAR_BIT - 1 doesn't
                        seem relevant to me, because unsigned char is already a special case
                        (as the only integer type that may not contain trap representations or
                        padding bits).
                        >
                        I can find no requirement that every possible bit combination must be
                        a valid value. The fact that a signed 9-bit char can support a value
                        larger than 173 in any of the three allowed representations doesn't
                        mean it has to.
                        Hmm, okay, there is explicit permission for what would otherwise be
                        negative zero to be a trap representation, but perhaps that is
                        redundant too.

                        The rationale does say that "any result of bitwise manipulation
                        produces an integer result which can be printed by printf", but since
                        that is already incorrect for other reasons, it may not apply to 99999
                        | 16384 either (which would overflow if INT_MAX is 99999).

                        Comment

                        • Army1987

                          #27
                          Re: print binary representation

                          The following example is from the Users' Reference to B by Ken Thompson:

                          /* The following function will print a non-negative number, n, to
                          the base b, where 2<=b<=10, This routine uses the fact that
                          in the ASCII character set, the digits 0 to 9 have sequential
                          code values. */

                          printn(n,b) {
                          extrn putchar;
                          auto a;

                          if(a=n/b) /* assignment, not test for equality */
                          printn(a, b); /* recursive */
                          putchar(n%b + '0');
                          }Simplily adding "void" before "printn", replacing "extrn putchar" with
                          "#include <stdio.h>" outside the function, and replace "auto" with "int"
                          translates it into C."Carramba" <user@example.n etha scritto nel messaggio
                          news:4604d5ca$0 $488$cc7c7865@n ews.luth.se...
                          Hi!
                          >
                          How can I output value of char or int in binary form with printf(); ?
                          >
                          thanx in advance

                          Comment

                          Working...