How to represent the biggest number in a system

B

Ben Bacarisse

Malcolm McLean said:
It is machine invented by a former reg called Kaz Kazsomethingorother - he
had an Arabic surname I never worked out how to pronounce - which implements
a C compiler as perversely as possible, though fully conforming.
So it would use sign magnitude representation instead of two's complement,
for instance.


Would naturally cause a trap representation and program termination.

Would it? I thought -1u (and David T Ashley's original (unsigned)-1)
are always valid unsigned values. The shift of an unsigned value
can't result in a trap representation. OK, -1u >> 1 is not always ==
INT_MAX, but it is always defined.
 
F

fdmfdmfdm

Why not use header files? That's what they're there for.
INT_MAX from <limits.h> is perfect for the purpose.
--
"For those who want to translate C to Pascal, it may be that a lobotomy
serves your needs better." --M. Ambuhl

"Here are the steps to create a C-to-Turbo-Pascal translator..." --H. Schildt

OMG, too many answers. Actually, my original intention is to ask: how
many bit will be in 0x7FFFFFFF, without considering leading 0s (let's
assume they are not added), I think this long integer is 32 bits, am I
right?
 
S

santosh

OMG, too many answers. Actually, my original intention is to ask: how
many bit will be in 0x7FFFFFFF, without considering leading 0s (let's
assume they are not added), I think this long integer is 32 bits, am I
right?

It's not a long integer. It's a numerical value in base 16. In C
source, it's treated as a hexadecimal constant value. Without a size
suffix, it's assigned the smallest integer type that can hold the
value. What that will be depends on your implementation. It might be
int or long. If you want the value to be treated as type long then add
the L or l suffix to it.

Atleast 32 bits are required to represent the value, but whether the
object that gets assigned the value in the source is int or long or
long long will depend on the programmer and the implementation limits.
In C, long is guaranteed to be atleast 32 bits, so it'll safely hold
the above value. An int may or may not, depending on it's range,
(which is 32 bits on most modern desktop processors).
 
J

Joe Wright

OMG, too many answers. Actually, my original intention is to ask: how
many bit will be in 0x7FFFFFFF, without considering leading 0s (let's
assume they are not added), I think this long integer is 32 bits, am I
right?
01111111 11111111 11111111 11111111

31 bits?
 
K

Keith Thompson

OMG, too many answers. Actually, my original intention is to ask: how
many bit will be in 0x7FFFFFFF, without considering leading 0s (let's
assume they are not added), I think this long integer is 32 bits, am I
right?

The value 0x7FFFFFFF has 31 1-bits, 4 for each 'F' and 3 for the '7'.
That means it requires at least a 32-bit signed type to store it
(adding 1 bit for the sign bit), or at least a 31-bit unsigned type.
(Yes, a 31-bit unsigned type is theoretically possible, with
CHAR_BIT==31 and sizeof(unsigned)==1, or with padding bits.)
 
R

Richard Bos

Malcolm McLean said:
It is machine invented by a former reg called Kaz Kazsomethingorother - he
had an Arabic surname I never worked out how to pronounce

Kaz Kylheku. And I think it's Slavic, not Arabic - IIRC the Kaz stands
for Kazimir.

Richard
 
J

Jorge Peixoto de Morais Neto

And how exactly do you expect to write portable code if you're going to
ignore thesystemprovisions that were designed to facilitate that goal?
You can do it in this case, but not in all cases.
The safest way to deal with this is to use the constants in <limits.h>.
The second-safest way

[With clear daylight behind the safest way...]
(since just about every platform is 2's complement
these days) is to use some expression that automatically sizes to the
native integer size. Off the top of my head,
((unsigned)-1 >> 1)
might work.

More simply (-1u >> 1).

It will work on vanilla machines, but it won't work on death stations.
UINT_MAX == INT_MAX is allowed. UINT_MAX > 4 * INT_MAX is
allowed. [UINT_MAX >= INT_MAX is required.]

It is possible to 'compute' the maximum value of int on a C90
implementation... [method from Lawrance Kirby]

int int_max(void)
{
unsigned u = -1;
int i;
while ((i = u) < 0 || i != u) u /= 2;
return i;
}

Pardon my ignorance, but what is the point of tesing i!=u right after
doing i=u?
 
G

Guest

Jorge said:
David T. Ashley said:
let's say without checking including files, how do we torepresentthe
biggestsay int in asystem?
And how exactly do you expect to write portable code if you're going to
ignore thesystemprovisions that were designed to facilitate that goal?
You can do it in this case, but not in all cases.
I ran across a book give this code:
long intbiggest= 0x7FFFFFFF;
The safest way to deal with this is to use the constants in <limits.h>.
The second-safest way

[With clear daylight behind the safest way...]
(since just about every platform is 2's complement
these days) is to use some expression that automatically sizes to the
native integer size. Off the top of my head,
((unsigned)-1 >> 1)
might work.

More simply (-1u >> 1).

It will work on vanilla machines, but it won't work on death stations.
UINT_MAX == INT_MAX is allowed. UINT_MAX > 4 * INT_MAX is
allowed. [UINT_MAX >= INT_MAX is required.]

It is possible to 'compute' the maximum value of int on a C90
implementation... [method from Lawrance Kirby]

int int_max(void)
{
unsigned u = -1;
int i;
while ((i = u) < 0 || i != u) u /= 2;
return i;
}

Pardon my ignorance, but what is the point of tesing i!=u right after
doing i=u?

It becomes clearer when you make some implicit conversions explicit:

while ((i = (signed int) u) < 0 || ((unsigned int) i) != u) u /= 2;

In other words, it tests whether the unsigned number has changed by
converting it to signed int.
 
O

Old Wolf

#include <stdio.h>

int main(void)
{
int i = (int)((-1U)>>1);
long l = (long)((-1UL)>>1);

printf("%x %lx\n", i, l);
}

1. Why the useless casts?
2. You cause UB by passing signed values for %x which expects
unsigned.
3. The value you compute is not guaranteed to be the maximum.
 
I

Ian Collins

Old said:
1. Why the useless casts?
2. You cause UB by passing signed values for %x which expects
unsigned.
3. The value you compute is not guaranteed to be the maximum.
You snipped The scene setting "something like the following abomination".
 

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
474,438
Messages
2,571,699
Members
48,796
Latest member
Greg L.
Top