accessing a integer of size 4bytes in a 16bit architecture

R

ramu

Hi,
Can you please tell me how to access a integer of size 4bytes in
a 16bit architecture?

Regards
 
T

Tom St Denis

unsigned long has to be at least 32 bits.
long can be as little as 31.9999999996640964- bits.

While I realize you're trying to be funny, long still represents 2^32
values, even though the absolute maximum is half that of an unsigned
long. So no, a long MUST be at least 32 bits.

Tom
 
T

Tim Rentsch

Tom St Denis said:
While I realize you're trying to be funny, long still represents 2^32
values, even though the absolute maximum is half that of an unsigned
long. So no, a long MUST be at least 32 bits.

The point of the joke is that long might have only (2**32 - 1) values
rather than (2**32) values.
 
N

Nick Keighley

long has to be at least 32 bits

oh, and to be kind(er) to the Original Poster. Your question contains
too little detail about what you are trying to do. If you just want to
store a 32 bit number use long. If you are accessing a bunch of bytes
in memory you may have to reconstruct the number from its constituant
bytes allowing for endianess (byte ordering). But we just can't tell.
I don't see what 16-bit architectures have to do with it.
 
N

Nobody

While I realize you're trying to be funny, long still represents 2^32
values, even though the absolute maximum is half that of an unsigned
long. So no, a long MUST be at least 32 bits.

Sign-bit representation is allowed, so it may only be able to represent
2^32-1 distinct values. log[2](2^32-1) = 31.9999999996640964...
 
B

bartc

Tim Rentsch said:
unsigned long has to be at least 32 bits.
long can be as little as 31.9999999996640964- bits.

I don't understand. Are you saying a signed value can be as little as one
bit if the representation uses 31 bits for a sign?
 
T

Tom St Denis

While I realize you're trying to be funny, long still represents 2^32
values, even though the absolute maximum is half that of an unsigned
long.  So no, a long MUST be at least 32 bits.

Sign-bit representation is allowed, so it may only be able to represent
2^32-1 distinct values. log[2](2^32-1) = 31.9999999996640964...

You still need a bit of entropy for the sign bit. And anyways ... 0
to 2^31 - 1 inclusive is 2^31 values, there are 2^31 negative values.
That's 2^32 unique values.

Maybe I'm not getting your math failures?

Tom
 
T

Tom St Denis

The point of the joke is that long might have only (2**32 - 1) values
rather than (2**32) values.

There are 2^n values between 0 and 2^n - 1 inclusively.

Stay in school kids.

Tom
 
J

James Kuyper

Tom said:
There are 2^n values between 0 and 2^n - 1 inclusively.


He was talking about a signed type, not an unsigned type. The C standard
allows the use of sign-magnitude or 1's complement representation, in
which the range of values that can be represented is 1-2^(n-1) to
2^(n-1)-1, for a total number of representable values of 2^n-1.
 
K

Keith Thompson

Tom St Denis said:
There are 2^n values between 0 and 2^n - 1 inclusively.

Yes, so *unsigned* long requires at least 32 bits. Specifically,
the standard requires LONG_MAX to be at least +2147483647, and
each integer value in the range 0..LONG_MAX to be representable;
with a binary representation, this is not possible unless long is
at least 32 bits.

But there can be as few as 2**32-1 distinct values for *signed*
long. The standard requires LONG_MIN <= -2147483647 and LONG_MAX
= +2147483647. (ones' complement and sign-and-magnitude
representations have two distinct representations for 0.)

The joke is that these 2**32-1 distinct values can be represented
in log2(2**32-1), or about 31.9999999996641, bits.
Stay in school kids.

Good advice.
 
K

Keith Thompson

Eric Sosman said:
Keith said:
[...]
The joke is that these 2**32-1 distinct values can be represented
in log2(2**32-1), or about 31.9999999996641, bits.

... but a representation involving "fractional bits" would not
meet the requirements of 6.2.6.2p2,

If it did, it wouldn't be a joke.
so can we drop this silly
sub-thread?

Oh, ok.
 
F

Flash Gordon

Richard said:
In
On Wed, 21 Oct 2009 04:50:20 -0700, Tom St Denis wrote:
long has to be at least 32 bits
unsigned long has to be at least 32 bits.
long can be as little as 31.9999999996640964- bits.
While I realize you're trying to be funny, long still represents
2^32 values, even though the absolute maximum is half that of an
unsigned long. So no, a long MUST be at least 32 bits.
Sign-bit representation is allowed, so it may only be able to
represent 2^32-1 distinct values. log[2](2^32-1) =
31.9999999996640964...
You still need a bit of entropy for the sign bit. And anyways ... 0
to 2^31 - 1 inclusive is 2^31 values, there are 2^31 negative
values. That's 2^32 unique values.

Maybe I'm not getting your math failures?

Maybe you're not getting *your* math failures - or rather, your C
failures. LONG_MIN can be as "high" as -2147483647; on such a system,
there are only 4294967295 distinct values, not 4294967296. That is
the whole point of the joke.

Note that this applies to 2s complement as well, so arguing that
negative zero and positive zero are different would not help. In 2s
complement the bit pattern that would normally represent -2147483648
could be a trap representation and thus not represent any value.
 
B

bartc

bartc said:
I don't understand. Are you saying a signed value can be as little as one
bit if the representation uses 31 bits for a sign?

OK. I think I've understood the, er, joke now.

The C standard specifies a restricted LONG_MIN value to allow a small number
of different sign representations, but not an arbitrary one (so I can't have
my 31-bit sign; I was thinking the standard only specified minimum 32 bits,
leaving LONG_MIN to the implementation).

It does however seem to allow a 2's-complement machine to have a spare value
for signed long (-2147483648), and still conform, which sounds handy. (At
least, until it meets some other software that takes -2147483648 values for
granted.)
 
T

Tom St Denis

In


On Wed, 21 Oct 2009 04:50:20 -0700, Tom St Denis wrote:
long has to be at least 32 bits
unsigned long has to be at least 32 bits.
long can be as little as 31.9999999996640964- bits.
While I realize you're trying to be funny, long still represents
2^32 values, even though the absolute maximum is half that of an
unsigned long.  So no, a long MUST be at least 32 bits.
Sign-bit representation is allowed, so it may only be able to
represent 2^32-1 distinct values. log[2](2^32-1) =
31.9999999996640964...
You still need a bit of entropy for the sign bit.  And anyways ... 0
to 2^31 - 1  inclusive is 2^31 values, there are 2^31 negative
values. That's 2^32 unique values.
Maybe I'm not getting your math failures?

Maybe you're not getting *your* math failures - or rather, your C
failures. LONG_MIN can be as "high" as -2147483647; on such a system,
there are only 4294967295 distinct values, not 4294967296. That is
the whole point of the joke.

Now I know why I stay out of asinine C threads...

Whatever, it's all academic anyways.

Tom
 
D

Dik T. Winter

> > Sign-bit representation is allowed, so it may only be able to represent
> > 2^32-1 distinct values. log[2](2^32-1) =3D 31.9999999996640964...
>
> You still need a bit of entropy for the sign bit. And anyways ... 0
> to 2^31 - 1 inclusive is 2^31 values, there are 2^31 negative values.
> That's 2^32 unique values.

But between 1 - 2^31 and 2^31 - 1 inclusive is 2^32 - 1 unique values.

On the other hand, I have encountered a 2's complement machine where
the most negative value was 1 - 2^31 because what would otherwise be
the most negative value acted as a trap representation.
 
T

Tom St Denis

...
 > > Sign-bit representation is allowed, so it may only be able to represent
 > > 2^32-1 distinct values. log[2](2^32-1) =3D 31.9999999996640964....
 >
 > You still need a bit of entropy for the sign bit.  And anyways ... 0
 > to 2^31 - 1  inclusive is 2^31 values, there are 2^31 negative values.
 > That's 2^32 unique values.

But between 1 - 2^31 and 2^31 - 1 inclusive is 2^32 - 1 unique values.

On the other hand, I have encountered a 2's complement machine where
the most negative value was 1 - 2^31 because what would otherwise be
the most negative value acted as a trap representation.

yeah yeah yeah I get it. It's all academic anyways since essentially
all platforms nowadays don't do that. All of ARM, PPC, MIPS, and x86
[which makes up the vast majority of 32/64-bit processors] have at
least a 32-bit range for signed longs.

But I guess my bad for not having the ISO spec memorized in all
aspects.

Tom
 
D

Dik T. Winter

> > But between 1 - 2^31 and 2^31 - 1 inclusive is 2^32 - 1 unique values.
> >
> > On the other hand, I have encountered a 2's complement machine where
> > the most negative value was 1 - 2^31 because what would otherwise be
> > the most negative value acted as a trap representation.
>
> yeah yeah yeah I get it. It's all academic anyways since essentially
> all platforms nowadays don't do that. All of ARM, PPC, MIPS, and x86
> [which makes up the vast majority of 32/64-bit processors] have at
> least a 32-bit range for signed longs.

Actually having a trap representation there can be pretty helpful in
detecting unassigned variables. And, of course, negation always works.
 
T

Tom St Denis

 > yeah yeah yeah I get it.  It's all academic anyways since essentially
 > all platforms nowadays don't do that.  All of ARM, PPC, MIPS, and x86
 > [which makes up the vast majority of 32/64-bit processors] have at
 > least a 32-bit range for signed longs.

Actually having a trap representation there can be pretty helpful in
detecting unassigned variables.  And, of course, negation always works.

Useful or not, most platforms can represent -2^31 in a long. 2^31 +
2^31 is 2^32. That I get that the spec says they need only support
-2^31 + 1 means I'm ok with getting schooled about my earlier
comments. But I stand by the fact it's an academic distinction.

It's like arguing about CHAR_BIT=8. Sure, don't ever say it's "equal
to 8" but you can pretty much guarantee that on a platform that was
fab'ed in the last 20 years and ISN'T A PIC of some sort, that it's a
sure thing.

Tom
 

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,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top