displaying underlying representation of integers

M

Mantorok Redgormor

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?
 
R

Richard Bos

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
 
C

CBFalconer

Mantorok said:
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;
}
 
J

Jack Klein

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
 
P

Peter Nilsson

Jack Klein said:
Should be...


...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.
 
P

pete

Mantorok said:
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 */
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top