print binary representation

G

Guest

Barry said:
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).
 
S

SM Ryan

# 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.
 
C

CBFalconer

Carramba said:
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_FAILURE);
}
binprt(x);
putchar('\n');
return 0;
}
 
B

Barry Schwarz

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.


Remove del for email
 
P

pete

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

Guest

Barry said:
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).
 
A

Army1987

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"
 

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,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top