| TIP: Click on subject to list as thread! | ANSI |
| echo: | |
|---|---|
| to: | |
| from: | |
| date: | |
| subject: | [C] typecasting |
From: "Kurt Kuzba"
From: Roger Scudder
RS> If the goal is (as you wrote) to
RS> 'get rid of the negative sign'
RS> take a look at the abs() function.
Or you could always roll your own on the fly.
#include
#include
int main(void)
{
int test = -33;
printf("(unsigned)test\t\t\t= %u\n", test);
printf("(unsigned)0-abs(test)\t\t= %u\n", 0 - abs(test));
printf("abs(test)\t\t\t= %u\n", test * -(test < 0));
printf("uint_max\t\t\t= %u\n", UINT_MAX);
printf("uint_max - (unsigned)test\t= %u\n",
UINT_MAX - (unsigned)test);
printf("uint_max + abs(test)\t\t= %u\n", UINT_MAX + abs(test));
return 0;
}
As you know, just printing a negative signed number which has
been cast to unsigned may result in an entirely erroneous result,
due to the way specific machine data architectures might use the
most significant bit of a signed number to indicate that it is
negative.
The example above should illustrate this oddity when run.
It really depends on your system architecture, though.
> kkuzba{at}centurytel.net http://home.centurytel.net/kkuzba
> When nothing is done, nothing is left undone. (Tao te Ching #48)
--- BBBS/LiI v4.01 Flag-5
* Origin: Prism's_Point (1:261/38.1)SEEN-BY: 633/267 270 @PATH: 261/38 123/500 106/2000 633/267 |
|
| SOURCE: echomail via fidonet.ozzmosis.com | |
Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.