Sign in

username:

password:



Not a member?

Search Comp.Arch.Embedded



Search tips

embedded by Keywords

68HC11 | 68HC12 | 8051 | 8052 | ARM | ARM7 | Asic | AT91 | AT91RM9200 | Atmel | AVR | AVRStudio | Bootloader | CFP | CompactFlash | Cygnal | Cypress | Dataflash | DSP | eCos | EEPROM | Embedded Linux | Emulator | Endian | Ethernet | Firewire | FPGA | Freescale | GCC | GNUARM | GSM | H8 | HDLC | I2C | Infineon | Interrupts | Java | JTAG | LCD | LED | LPC2000 | MCU | Microchip | MMC | MPLAB | MSP430 | PC104 | PCB | PCI | PCMCIA | PowerPC | Rabbit | RS232 | RS485 | RTOS | SBC | SDRAM | Sensor | SPI | STK500 | UART | UML | USART | USB | Verilog | VHDL | VxWorks | Xilinx

Ads

Discussion Groups

Discussion Groups | Comp.Arch.Embedded | Simple C question...entering binary

There are 45 messages in this thread.

You are currently looking at messages 20 to 30.

Re: Simple C question...entering binary - Grant Edwards - 00:35 29-01-08

On 2008-01-29, Eric Smith <e...@brouhaha.com> wrote:
>>>>> to get all 1s, better is ~0
>>>> How is that better?
>>> should work on 1s-complement machines.
>
> CBFalconer <c...@yahoo.com> writes:
>> So does "unsigned int u = -1;".
>
> On a 1's complement machine, that will result in the variable
> u having all bits set other than the least signficant bit,
> since that is the one's complement representation of -1.

Are any of gcc's targets 1's compliment?

-- 
Grant Edwards                   grante             Yow!  I feel like a wet
                                  at               parking meter on Darvon!
                               visi.com            



Re: Simple C question...entering binary - Arlet - 01:42 29-01-08

On Jan 29, 4:31 am, Eric Smith <e...@brouhaha.com> wrote:
> Arlet Ottens <usene...@c-scape.nl> writes:
> > According to C99, 1's complement versus 2's complement, or even 'sign
> > and magnitude' notations only apply to _signed_ integer types.
>
> Sure.  So your -1 literal is a one's complement signed literal, represented
> with all bits one except the LSB.  Now you assign it to your unsigned
> variable, causing an implicit cast, and you still have the same bit pattern
> as your signed constant.

No. An implicit cast from signed to unsigned does not imply that the
bit pattern will be unchanged.

Section 6.3.1.3 from the C99 standard:

"2. Otherwise, if the new type is unsigned, the value is converted by
repeatedly adding or subtracting one more than the maximum value that
can be represented in the new type until the value is in the range of
the new type."

Re: Simple C question...entering binary - Stefan Reuther - 13:34 29-01-08

Eric Smith wrote:
>>>>>to get all 1s, better is ~0
>>>>
>>>>How is that better?
>>>
>>>should work on 1s-complement machines.
> 
> CBFalconer <c...@yahoo.com> writes:
> 
>>So does "unsigned int u = -1;".
> 
> On a 1's complement machine, that will result in the variable u having all
> bits set other than the least signficant bit, since that is the one's
> complement representation of -1.

That would be the case if you cast a bit pattern, as in
   *(int*) &u = -1;
If you just do
   u = -1;
you will always get 'all-bits-one', no matter what the actual
representation ist. At least, that's what the Holy Standard says in
6.3.1.3p2.


  Stefan


Re: Simple C question...entering binary - Eric Smith - 14:48 29-01-08

Grant Edwards <g...@visi.com> writes:
> Are any of gcc's targets 1's compliment?

Not that I've ever heard of.  Maybe someone has hacked it to target
a Unisys ClearPath (descendent of Univac 1107).


Re: Simple C question...entering binary - Eric Smith - 14:51 29-01-08

Stefan Reuther <s...@arcor.de> writes:
> That would be the case if you cast a bit pattern, as in
>    *(int*) &u = -1;
> If you just do
>    u = -1;
> you will always get 'all-bits-one', no matter what the actual
> representation ist. At least, that's what the Holy Standard says in
> 6.3.1.3p2.

I must be particuarly dense today, because I don't see how you get from
the wording of 6.3.1.3p2 to the conclusion that you're guaranteed that
assigning negative one to an unsigned variable guarantees that the latter
will have all bits set.

Re: Simple C question...entering binary - Eric Smith - 15:00 29-01-08

I wrote:
> I must be particuarly dense today, because I don't see how you get from
> the wording of 6.3.1.3p2 to the conclusion that you're guaranteed that
> assigning negative one to an unsigned variable guarantees that the latter
> will have all bits set.

Never mind, I see it now.

Whoever thought that up obviously never really considered what a compiler
for a one's complement machine would have to do to implement this:

    unsigned a;
    int b;
    [...]
    a = b;

to handle the case where b is negative at the time of the assignment.
(For the case of a literal or other static value, it can be dealt with
at compile time.)

The emitted code for the assignment will actually have to test for b being
negative and increment the representation.

Sigh.

Re: Simple C question...entering binary - msg - 15:20 29-01-08

Eric Smith wrote:

> Grant Edwards <g...@visi.com> writes:
> 
>>Are any of gcc's targets 1's compliment?
> 
> 
> Not that I've ever heard of.  Maybe someone has hacked it to target
> a Unisys ClearPath (descendent of Univac 1107).
> 

I don't have my old manuals handy; was the Unisys 10000 series
1's complement?

Michael

Re: Simple C question...entering binary - =?ISO-8859-1?Q?Hans-Bernhard_Br=F6ker?= - 15:21 29-01-08

Eric Smith wrote:
> Stefan Reuther <s...@arcor.de> writes:
>> That would be the case if you cast a bit pattern, as in
>>    *(int*) &u = -1;
>> If you just do
>>    u = -1;
>> you will always get 'all-bits-one', no matter what the actual
>> representation ist. At least, that's what the Holy Standard says in
>> 6.3.1.3p2.
> 
> I must be particuarly dense today, because I don't see how you get from
> the wording of 6.3.1.3p2 to the conclusion that you're guaranteed that
> assigning negative one to an unsigned variable guarantees that the latter
> will have all bits set.

6.3.1.3p2 grants that the value you get will be the maximum value the 
unsigned type can hold, i.e.

	unsigned int u = -1;

yields UINT_MAX.  That's what you get by adding (UINT_MAX + 1) once to 
the input value of (-1), to arrive at a value inside the representable 
range of [0 ..UINT_MAX].

6.2.6.2 effectively defines that UINT_MAX must be of the form (2^n - 1), 
i.e. all-bits-one.  Well, "all-value-bits-one", to be fully picky.


Re: Simple C question...entering binary - =?ISO-8859-1?Q?Hans-Bernhard_Br=F6ker?= - 15:24 29-01-08

Eric Smith wrote:

> Sure.  So your -1 literal is a one's complement signed literal, represented
> with all bits one except the LSB.  Now you assign it to your unsigned
> variable, causing an implicit cast, and you still have the same bit pattern
> as your signed constant.

No.  Casts are defined in terms of the _value_, not the representation.

> Sure.  But the implict cast doesn't change the representation, only the
> interpretation.  

It's exactly the other way round.

Re: Simple C question...entering binary - =?ISO-8859-1?Q?Hans-Bernhard_Br=F6ker?= - 16:29 29-01-08

Eric Smith wrote:

> Whoever thought that up obviously never really considered what a compiler
> for a one's complement machine would have to do to implement this:

What do you base this belief on?  Are we to assume you're a telepathic 
time traveler, or how else would you know what people in a conference 
room did or didn't think, most of twenty years ago?

> The emitted code for the assignment will actually have to test for b being
> negative and increment the representation.

Yes.  So what?

previous | 1 | 2 | 3 | 4 | 5 | next