Re: Type conversion



In article <m3y7a6cfzp.fsf@xxxxxxxxxxxxxxx>,
Mark L Pappin <mlp@xxxxxxx> wrote:

int a = 0x12345678;
char b;
b=(char)a;

Would the value of b depend on the endianness of the machine?

Yes.

Casts, like most other operations, are defined in terms of value, not
representation, no? Thus the low-order 8 bits will be used, and in
this instance since
(a & 0xff) == (a & 0x7f)
even the signedness of 'char' is irrelevant as values up to 127 are
guaranteed representable even in 'signed char'.

If char is signed, we have a conversion to a signed integer with
smaller size, and in that case if the value cannot be represented
the result is implementation-defined.

-- Richard
--
:wq
.



Relevant Pages

  • Re: Why not just have int8, int16, int32, int64, etc?
    ... Objects declared as characters char) shall be large enough to store ... representation participate in the value representation. ... sentation as its corresponding unsigned integer type. ... the same size, signedness, and alignment requirements ...
    (comp.lang.cpp)
  • Re: unsigned char, char, signed char and all that
    ... collection of N bits which use a binary representation to represent ... to be accessed by aliasing the object to array of unsigned char. ... Every bit that is a value bit in signed char, ...
    (comp.std.c)
  • Re: Problem with Bit operations
    ... If, on your implementation, "char" is signed, then then assignment will ... You are counting on a particular bit representation for a negative ... Notice that in 2s complement signed character representation in which 8 ... 0x notation merely indicates hexidecimal representation. ...
    (comp.lang.c)
  • Re: print binary representation
    ... How can I output value of char or int in binary form with printf;? ... It accounts for ones' complement, where all bits 1 is a possible ... sign bit 1 is a representation of 0. ...
    (comp.lang.c)
  • Re: How to: referencing variables using the contents of otehr variables.
    ... each cell contains one char, ... A White King is 'K", a black king is 'k', and so ... ChessPiece{representation = rep;} ...
    (comp.lang.java.help)