Re: Bit Pattern Problem

From: Alex Fraser (me_at_privacy.net)
Date: 09/02/04


Date: Thu, 2 Sep 2004 11:17:18 +0100


"pete" <pfiland@mindspring.com> wrote in message
news:4136EAC9.3A5C@mindspring.com...
> William L. Bahn wrote:
>
> > As assignments between signed and unsigned data types of the same
> > basic type required to copy the bit pattern if the value in the
> > expression can't be represented in the type of the object being
> > assigned to? Or is this undefined behavior?
>
> Assigning an out of range value to a signed integer,
> is implementation defined.
> To assign a negative value to an unsigned,
> (UINT_MAX + 1) is added to the value until it becomes non negative.

(or, obviously, ULONG_MAX + 1 etc, according to the type of the object being
assigned to.)

...which, if the negative value can be represented using two's complement in
the number of value bits in the unsigned integer, leaves the unsigned
integer with the two's complement representation of the negative value.

In many cases this means that assignment of a signed integer type to the
corresponding unsigned integer type does indeed simply copy the bit pattern.

> To assign an out of range positive value to an unsigned,
> (UINT_MAX + 1) is subtracted until the value is in range.

Alex



Relevant Pages

  • Re: How to find and replace sequences of elements
    ... Yesterday, however, I added Stylesheet assignments, i.e. ... > When I now apply the pattern, it does what it is supposed to do, but, ... > non-well-formed XML because there are two root elements). ... > XSL assignment, ...
    (comp.text.xml)
  • Re: Konstanten immer links beim Vergelich?
    ... "Timo Stamm" ... den Rückgabewert eines Assignments auf void gesetzt. ... Ist ein gängiges Pattern und würde mir sicher ... fehlen. ...
    (de.comp.lang.java)
  • Re: How to find and replace sequences of elements
    ... Yesterday, however, I added Stylesheet assignments, i.e. ... to my XML documents. ... When I now apply the pattern, it does what it is supposed to do, but, ... XSL assignment, ...
    (comp.text.xml)
  • Re: Bit Pattern Problem
    ... William L. Bahn wrote: ... > As assignments between signed and unsigned data types of the same ... > basic type required to copy the bit pattern if the value in the ...
    (comp.lang.c)