Article View: comp.std.c
Article #3650Re: short division question? ( short ) 0x8000 / ( short ) 0xFFFF = ?
From: Clem Taylor
Date: Fri, 04 Aug 1995 16:39
Date: Fri, 04 Aug 1995 16:39
47 lines
2032 bytes
2032 bytes
In article <1995Aug3.165450.5750@sq.sq.com>, Mark Brader <msb@sq.sq.com> wrote:
>Clem Taylor (ctaylor@stimpy.eecis.udel.edu) writes:
>> main ( )
>> {
>> short a = 0x8000, b = 0xFFFF, c; /* signed 16 bit values */
>
...
>In practice this conversion is typically done by copying the 16 low bits,
>so that on a 2's complement machine we get -32768 for a and -1 for b,
>but I don't believe there's anything in the standard that requires it to
>be implemented in any particular way. However, from here on I will assume
>that the values *are* -32768 and -1.
I think the example I gave did not completely describe hardware involved.
The machine in question is a 16 bit processor where 0x8000 is -32768 and
0xFFFF is also -1.
On the machine in question, the following program:
main ( )
{
int a = 0x8000, b = 0xFFFF, c; /* ints are 16 bits */
int x = -32768, y = -1, z;
c = a / b;
z = x / y;
printf ( "a=%hd b=%hd c=%hd [a=0x%04hX b=0x%04hX c=0x%04hX]\n",
a, b, c, a, b, c );
printf ( "x=%hd y=%hd z=%hd [x=0x%04hX y=0x%04hX z=0x%04hX]\n",
a, b, c, x, y, z );
}
This returns:
a=-32768 b=-1 c=32767 [a=0x8000 b=0xFFFF c=0x7FFF]
x=-32768 y=-1 z=32767 [x=0x8000 y=0xFFFF z=0x7FFF]
If you run the same program on a 32 bit machine with shorts, you get
a=-32768 b=-1 c=-32768 [a=0x8000 b=0xFFFF c=0x8000]
x=-32768 y=-1 z=-32768 [x=0x8000 y=0xFFFF z=0x8000]
So, on a 16 bit machine, where promotion is not an issue, is this behavior
correct? Does the standard specify this...
Many thanks,
Clem
Technical Details
Message-ID:
<3vtig7$9mc@louie.udel.edu>Path:
arc1-ca.pugleaf.net!stimpy.eecis.udel.edu!ctaylorReferences:
<3vqpv8$fo0@louie.udel.edu> <1995Aug3.165450.5750@sq.sq.com>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!stimpy.eecis.udel.edu!ctaylor From: ctaylor@stimpy.eecis.udel.edu (Clem Taylor) Newsgroups: comp.std.c Subject: Re: short division question? ( short ) 0x8000 / ( short ) 0xFFFF = ? Date: 4 Aug 1995 16:39:35 GMT Organization: University of Delaware, Newark Message-ID: <3vtig7$9mc@louie.udel.edu> References: <3vqpv8$fo0@louie.udel.edu> <1995Aug3.165450.5750@sq.sq.com> NNTP-Posting-Host: stimpy-fddi.udel.edu