range limitation

P

Patrick

I have the following problem,

(1) I have an 8-Bit microcomputer
(2) It has an integer word size of 2bytes (=16 bits)
(3) So int type is in the range -32768 to +32767

(4) Now I want to add numbers of type int, in the range 0 to 50000

How do I deal with the range limitation here?
I can ONLY use int type.

any help appreciated!

pat
 
R

Régis Troadec

"Patrick" <[email protected]> a écrit dans le message de

Hi,
I have the following problem,

(1) I have an 8-Bit microcomputer
(2) It has an integer word size of 2bytes (=16 bits)
(3) So int type is in the range -32768 to +32767

(4) Now I want to add numbers of type int, in the range 0 to 50000

Is your system complies with any C standard?
How do I deal with the range limitation here?
I can ONLY use int type.

I'm not sure about what you mean by using only the int type, since the
storage of the unsigned int type (probably a range from 0 to 65535 according
your third point) is the same of the int type, altough they are different.

Regis
 
J

Jack Klein

I have the following problem,

(1) I have an 8-Bit microcomputer
(2) It has an integer word size of 2bytes (=16 bits)
(3) So int type is in the range -32768 to +32767

(4) Now I want to add numbers of type int, in the range 0 to 50000

How do I deal with the range limitation here?
I can ONLY use int type.

any help appreciated!

pat

Get a new job, your current one is unreasonable.

If you have a C compiler, it supports signed and unsigned longs, which
must have at least 32 bits. If it does not support signed and
unsigned long, it is not a C compiler, no matter what the people who
make it say.
 
L

Lew Pitcher

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
I have the following problem,

(1) I have an 8-Bit microcomputer
(2) It has an integer word size of 2bytes (=16 bits)
(3) So int type is in the range -32768 to +32767

(4) Now I want to add numbers of type int, in the range 0 to 50000

How do I deal with the range limitation here?
I can ONLY use int type.

In your implementation, int can hold one of 65,536 possible values
You have a requirement to be able to hold at least 50,001 possible values in an int.
What's the problem?

I can see two alternatives, and I'm sure that there are more.
1) store your values in an int, using excess-32768 notation, or
2) use unsigned int values, which on your implementation /should/ be
able to range from 0 to 65,535 (15,535 more than you require)

any help appreciated!

pat


- --
Lew Pitcher
IT Consultant, Enterprise Application Architecture,
Enterprise Technology Solutions, TD Bank Financial Group

(Opinions expressed are my own, not my employers')
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (MingW32)

iD8DBQFAjPxaagVFX4UWr64RAvhGAKDhi07pgP3YGRYty7SGi4t+tI5/PwCfe/3T
/9Qtwys3uQLdidqB7f50EkY=
=Rqi5
-----END PGP SIGNATURE-----
 
A

Alex

[snip]
I can see two alternatives, and I'm sure that there are more.
1) store your values in an int, using excess-32768 notation, or
2) use unsigned int values, which on your implementation /should/ be
able to range from 0 to 65,535 (15,535 more than you require)

That's a 'must', not a 'should'; 65535 is the minimum for UINT_MAX (and
USHRT_MAX).
 
K

Keith Thompson

I have the following problem,

(1) I have an 8-Bit microcomputer
(2) It has an integer word size of 2bytes (=16 bits)
(3) So int type is in the range -32768 to +32767

(4) Now I want to add numbers of type int, in the range 0 to 50000

How do I deal with the range limitation here?
I can ONLY use int type.

Why can you only use int?

We see a lot of questions here of the form:

I need to do <some task> *without* using <the most obvious tool>.
How can I do this?

In your case, the most obvious tool is unsigned int (guaranteed to
have a range of at least 0 to 65535) or long (guaranteed to have a
range of at least -2147483647 to +2147483647).

We can be a lot more helpful if we know *why* you "can ONLY use int
type". If it's a puzzle, some of us might actually be interested in
helping you to solve it. If there's a good reason for the
restriction, knowing what that reason is will help us guess what other
constraints might or might not apply. If it's a homework assignment
(as some, but not all, of these questions turn out to be), the point
is for you to figure it out for yourself, though we'd be happy to
submit solutions directly to your instructor on your behalf. If it's
not a homework assignment, please ignore the previous sentence, which
was deliberately sarcastic.
 

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,141
Messages
2,570,817
Members
47,367
Latest member
mahdiharooniir

Latest Threads

Top