displaying underlying representation of integers

Discussion in 'C Programming' started by Mantorok Redgormor, Sep 26, 2003.

  1. Is it possible to display the underlying representation of integers?
    Not just integers but also float/double and is it also possible to
    display the padding bits of signed integer types in standard C?
     
    Mantorok Redgormor, Sep 26, 2003
    #1
    1. Advertising

  2. Mantorok Redgormor

    Richard Bos Guest

    (Mantorok Redgormor) wrote:

    > Is it possible to display the underlying representation of integers?
    > Not just integers but also float/double and is it also possible to
    > display the padding bits of signed integer types in standard C?


    Sure. Just treat everything as an array of unsigned chars and dump the
    raw bytes to the screen:

    #include <stdio.h>

    int main(void)
    {
    double test=12345.67;
    unsigned char *bytes;
    int n;

    for (n=0, bytes=&test; n<sizeof(test); n++)
    printf("Byte nr. %d of test has the value %x\n", n, bytes[n]);

    return 0;
    }

    Richard
     
    Richard Bos, Sep 26, 2003
    #2
    1. Advertising

  3. Mantorok Redgormor

    CBFalconer Guest

    Mantorok Redgormor wrote:
    >
    > Is it possible to display the underlying representation of integers?
    > Not just integers but also float/double and is it also possible to
    > display the padding bits of signed integer types in standard C?


    Note that the actual representation is implementation defined. I
    believe the following will do it, and should be fully portable.

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

    int main(void)
    {
    int i = 123;
    float f = 123.0;
    double d = 123.0;

    char *cp;
    size_t j;
    int n;
    unsigned int mask;

    n = (CHAR_BIT + 3) / 4;
    mask = (1 << CHAR_BIT) - 1;

    cp = (char *)&i;
    for (j = 0; j < sizeof(int); j++) {
    printf("%*x ", n, *(cp + j) & mask);
    }
    printf("\n");

    cp = (char *)&f;
    for (j = 0; j < sizeof(float); j++) {
    printf("%*x ", n, *(cp + j) & mask );
    }
    printf("\n");

    cp = (char *)&d;
    for (j = 0; j < sizeof(double); j++) {
    printf("%*x ", n, *(cp + j) & mask);
    }
    printf("\n");

    return 0;
    }

    --
    Chuck F () ()
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net> USE worldnet address!
     
    CBFalconer, Sep 26, 2003
    #3
  4. Mantorok Redgormor

    Jack Klein Guest

    On Fri, 26 Sep 2003 14:37:19 GMT, CBFalconer <>
    wrote in comp.lang.c:

    > Mantorok Redgormor wrote:
    > >
    > > Is it possible to display the underlying representation of integers?
    > > Not just integers but also float/double and is it also possible to
    > > display the padding bits of signed integer types in standard C?

    >
    > Note that the actual representation is implementation defined. I
    > believe the following will do it, and should be fully portable.


    Should be...

    > #include <stdio.h>
    > #include <limits.h>
    >
    > int main(void)
    > {
    > int i = 123;
    > float f = 123.0;
    > double d = 123.0;
    >
    > char *cp;
    > size_t j;
    > int n;
    > unsigned int mask;
    >
    > n = (CHAR_BIT + 3) / 4;
    > mask = (1 << CHAR_BIT) - 1;
    >
    > cp = (char *)&i;
    > for (j = 0; j < sizeof(int); j++) {


    ....but C99 allows signed char to have padding bits and trap
    representations. Better to use unsigned char.

    > printf("%*x ", n, *(cp + j) & mask);
    > }
    > printf("\n");
    >
    > cp = (char *)&f;
    > for (j = 0; j < sizeof(float); j++) {
    > printf("%*x ", n, *(cp + j) & mask );
    > }
    > printf("\n");
    >
    > cp = (char *)&d;
    > for (j = 0; j < sizeof(double); j++) {
    > printf("%*x ", n, *(cp + j) & mask);
    > }
    > printf("\n");
    >
    > return 0;
    > }


    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c /faq
     
    Jack Klein, Sep 26, 2003
    #4
  5. "Jack Klein" <> wrote in message
    news:...
    > On Fri, 26 Sep 2003 14:37:19 GMT, CBFalconer <>
    > wrote in comp.lang.c:
    >
    > > Mantorok Redgormor wrote:
    > > >
    > > > Is it possible to display the underlying representation of integers?
    > > > Not just integers but also float/double and is it also possible to
    > > > display the padding bits of signed integer types in standard C?

    > >
    > > Note that the actual representation is implementation defined. I
    > > believe the following will do it, and should be fully portable.

    >
    > Should be...
    >
    > > #include <stdio.h>
    > > #include <limits.h>
    > >
    > > int main(void)
    > > {
    > > int i = 123;
    > > float f = 123.0;
    > > double d = 123.0;
    > >
    > > char *cp;
    > > size_t j;
    > > int n;
    > > unsigned int mask;
    > >
    > > n = (CHAR_BIT + 3) / 4;
    > > mask = (1 << CHAR_BIT) - 1;
    > >
    > > cp = (char *)&i;
    > > for (j = 0; j < sizeof(int); j++) {

    >
    > ...but C99 allows signed char to have padding bits and trap
    > representations.


    Possibly more likely is the fact that signed char may not be two's
    complement. So, a negative zero representation will be incorrectly reported
    as a null byte in the printf below.

    > Better to use unsigned char.
    >
    > > printf("%*x ", n, *(cp + j) & mask);
    > > }
    > > printf("\n");


    --
    Peter
     
    Peter Nilsson, Sep 27, 2003
    #5
  6. Mantorok Redgormor

    pete Guest

    Mantorok Redgormor wrote:
    >
    > Is it possible to display the underlying representation of integers?
    > Not just integers but also float/double and is it also possible to
    > display the padding bits of signed integer types in standard C?


    /* BEGIN bitstr.c */

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

    typedef float e_type;
    #define STRING "%s = %f\n"

    char *bitstr(char *, void const *, size_t);

    int main(void)
    {
    e_type f, d;
    char fbits[CHAR_BIT * sizeof f + 1], *s;

    s = STRING;
    for (f = 0.2f; 0.75 > f; f += 0.125) {
    bitstr(fbits, &f, sizeof f);
    printf(s, fbits, f);
    }
    for (d = 2; 20000 > d; d *= 2) {
    for (f = d - 1; 0.75 > f - d; f += 0.5) {
    bitstr(fbits, &f, sizeof f);
    printf(s, fbits, f);
    }
    }
    return 0;
    }

    char *bitstr(char *str, const void *obj, size_t n)
    {
    unsigned char mask;
    const unsigned char *byte = obj;
    char *const ptr = str;

    while (n--) {
    mask = ((unsigned char)-1 >> 1) + 1;
    do {
    *str++ = (char)(mask & byte[n] ? '1' : '0');
    mask >>= 1;
    } while (mask);
    }
    *str = '\0';
    return ptr;
    }

    /* END bitstr.c */


    --
    pete
     
    pete, Sep 27, 2003
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mantorok Redgormor

    Representation of integers

    Mantorok Redgormor, Sep 13, 2003, in forum: C Programming
    Replies:
    10
    Views:
    631
    Kevin D. Quitt
    Sep 17, 2003
  2. Mantorok Redgormor

    representation of integers(again) very annoying..

    Mantorok Redgormor, Oct 13, 2003, in forum: C Programming
    Replies:
    4
    Views:
    464
    Barry Schwarz
    Oct 14, 2003
  3. =?iso-8859-9?Q?Tongu=E7?= Yumruk

    Bitmask representation of integers

    =?iso-8859-9?Q?Tongu=E7?= Yumruk, Oct 8, 2003, in forum: Python
    Replies:
    3
    Views:
    876
    Scott David Daniels
    Oct 8, 2003
  4. Guest
    Replies:
    3
    Views:
    378
    Cameron Laird
    Sep 9, 2004
  5. Shao Miller

    Re: Representation of integers

    Shao Miller, Jan 17, 2013, in forum: C Programming
    Replies:
    9
    Views:
    182
    Ben Bacarisse
    Jan 18, 2013
Loading...

Share This Page