From unsigned int to unsigned char
Author Message
From unsigned int to unsigned char

What is the result of a casting of unsigned int to unsigned char?
For example, if
---
unsigned int x = 0xAAAA;
unsigned char y;

y = (unsigned char)x;
---
what is the value of y? Is it standard or it depends from architecture?

Mon, 22 Aug 2005 04:04:56 GMT
From unsigned int to unsigned char

Quote:

> What is the result of a casting of unsigned int to unsigned char?
> For example, if
> ---
>     unsigned int x = 0xAAAA;
>     unsigned char y;

>     y = (unsigned char)x;
> ---
> what is the value of y?
> Is it standard or it depends from architecture?

The value of y depends on the value of CHAR_BIT.

--
pete

Mon, 22 Aug 2005 04:25:25 GMT
From unsigned int to unsigned char
I'm pretty sure the compiler tries to return the lowest order byte for
whatever architecture.

Quote:
> What is the result of a casting of unsigned int to unsigned char?
> For example, if
> ---
>     unsigned int x = 0xAAAA;
>     unsigned char y;

>     y = (unsigned char)x;
> ---
> what is the value of y? Is it standard or it depends from architecture?

--
--------------------------------------
Daniel Waites

Mon, 22 Aug 2005 04:25:43 GMT
From unsigned int to unsigned char

Quote:
> What is the result of a casting of unsigned int to unsigned char?
> For example, if
> ---
>     unsigned int x = 0xAAAA;
>     unsigned char y;

>     y = (unsigned char)x;
> ---
> what is the value of y? Is it standard or it depends from architecture?

ISO/IEC 9899:1999 (E)

6.3.1.3 Signed and unsigned integers

1 When a value with integer type is converted to another
integer type other than _Bool, if the value can be
represented by the new type, it is unchanged.

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. (49)

3 Otherwise, the new type is signed and the value cannot be
represented in it; either the result is implementation-
defined or an implementation-defined signal is raised.

(49) The rules describe arithmetic on the mathematical value,
not the value of a given type of expression.

-Mike

Mon, 22 Aug 2005 04:39:26 GMT
From unsigned int to unsigned char

Quote:

> What is the result of a casting of unsigned int to unsigned char?
> For example, if
> ---
>     unsigned int x = 0xAAAA, xx;
>     unsigned char y;

>     y = (unsigned char)x;
> ---
> what is the value of y? Is it standard or it depends from
> architecture?

It depends.  What is the value of CHAR_BIT on your system?  Add xx
as above, and then see what "xx = y;" yields via printf("%x\n",
xx);  You might also try printf("%d\n", CHAR_BIT); after #include
<limits.h>

--

Available for consulting/temporary embedded and systems.

Mon, 22 Aug 2005 05:55:46 GMT
From unsigned int to unsigned char

Quote:
> What is the result of a casting of unsigned int to unsigned char?
> For example, if
> ---
>     unsigned int x = 0xAAAA;
>     unsigned char y;

>     y = (unsigned char)x;
> ---
> what is the value of y? Is it standard or it depends from architecture?

The minimum guaranteed value is 0xAA. It can be more, according to the
architecture. If you want to portably limit the range (say 0..0xFF), you

y = (unsigned char) (x & 0xFF);

Note that AFAIK, the cast is unnecessary:

y = x & 0xFF;

is fine.

--
-ed- emdel at noos.fr
The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.html
C-library: http://www.dinkumware.com/htm_cl/index.html
FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/

Mon, 22 Aug 2005 06:28:39 GMT
From unsigned int to unsigned char

Wed, 18 Jun 1902 08:00:00 GMT
From unsigned int to unsigned char

Quote:
> What is the result of a casting of unsigned int to unsigned char?
> For example, if
> ---
>     unsigned int x = 0xAAAA;
>     unsigned char y;

>     y = (unsigned char)x;
> ---
> what is the value of y? Is it standard or it depends from architecture?

-----= Posted via Newsfeed.Com, Uncensored Usenet News =-----
http://www.newsfeed.com - The #1 Newsgroup Service in the World!

Mon, 22 Aug 2005 08:24:47 GMT
From unsigned int to unsigned char

Wed, 18 Jun 1902 08:00:00 GMT
From unsigned int to unsigned char

Quote:
>What is the result of a casting of unsigned int to unsigned char?
>For example, if
>---
>    unsigned int x = 0xAAAA;
>    unsigned char y;

>    y = (unsigned char)x;
>---
>what is the value of y? Is it standard or it depends from architecture?

It is both standard and architecture dependent: 0xAAAA % (UCHAR_MAX + 1).
UCHAR_MAX is defined in <limits.h>.

Dan
--
Dan Pop
DESY Zeuthen, RZ group

Mon, 22 Aug 2005 21:21:08 GMT
From unsigned int to unsigned char

Quote:
> What is the result of a casting of unsigned int to unsigned char?
> For example, if
> ---
>     unsigned int x = 0xAAAA;
>     unsigned char y;

>     y = (unsigned char)x;
> ---
> what is the value of y?

The expression is equivalent to both of these...

y = x & UCHAR_MAX;
y = x & (unsigned) (unsigned char) -1;

Quote:
> Is it standard or it depends from architecture?

It is standard and it depends on the implementation (which is likely to be
tuned to the architecture but there have been exceptions in the past).

--
Peter

Mon, 22 Aug 2005 16:40:00 GMT

 Page 1 of 1 [ 11 post ]

Relevant Pages