Jump to content

Unsigned Casting C

CJPowell27

hey guys, I've been trying to figure out signed and unsigned integer casting in C lately and I'm kinda stuck.  If i cast a number like -3 as unsigned, what would it turn out to be? I'm trying to evaluate whether (unsigned) -3 > 35 is true or false and I just can't figure out the casting. Any help is appreciated, thanks.

i5 4670k| Asrock H81M-ITX| EVGA Nex 650g| WD Black 500Gb| H100 with SP120s| ASUS Matrix 7970 Platinum (just sold)| Patriot Venom 1600Mhz 8Gb| Bitfenix Prodigy. Build log in progress 

Build Log here: http://linustechtips.com/main/topic/119926-yin-yang-prodigy-update-2-26-14/

Link to comment
Share on other sites

Link to post
Share on other sites

10 minutes ago, CJPowell27 said:

hey guys, I've been trying to figure out signed and unsigned integer casting in C lately and I'm kinda stuck.  If i cast a number like -3 as unsigned, what would it turn out to be? I'm trying to evaluate whether (unsigned) -3 > 35 is true or false and I just can't figure out the casting. Any help is appreciated, thanks.

http://stackoverflow.com/questions/50605/signed-to-unsigned-conversion-in-c-is-it-always-safe

Mattering on platform that number can be absolutely massive. So that -3 would be like subtracting 2 from the largest unsigned int available on the platform.

Main Gaming PC - i9 10850k @ 5GHz - EVGA XC Ultra 2080ti with Heatkiller 4 - Asrock Z490 Taichi - Corsair H115i - 32GB GSkill Ripjaws V 3600 CL16 OC'd to 3733 - HX850i - Samsung NVME 256GB SSD - Samsung 3.2TB PCIe 8x Enterprise NVMe - Toshiba 3TB 7200RPM HD - Lian Li Air

 

Proxmox Server - i7 8700k @ 4.5Ghz - 32GB EVGA 3000 CL15 OC'd to 3200 - Asus Strix Z370-E Gaming - Oracle F80 800GB Enterprise SSD, LSI SAS running 3 4TB and 2 6TB (Both Raid Z0), Samsung 840Pro 120GB - Phanteks Enthoo Pro

 

Super Server - i9 7980Xe @ 4.5GHz - 64GB 3200MHz Cl16 - Asrock X299 Professional - Nvidia Telsa K20 -Sandisk 512GB Enterprise SATA SSD, 128GB Seagate SATA SSD, 1.5TB WD Green (Over 9 years of power on time) - Phanteks Enthoo Pro 2

 

Laptop - 2019 Macbook Pro 16" - i7 - 16GB - 512GB - 5500M 8GB - Thermal Pads and Graphite Tape modded

 

Smart Phones - iPhone X - 64GB, AT&T, iOS 13.3 iPhone 6 : 16gb, AT&T, iOS 12 iPhone 4 : 16gb, AT&T Go Phone, iOS 7.1.1 Jailbroken. iPhone 3G : 8gb, AT&T Go Phone, iOS 4.2.1 Jailbroken.

 

Link to comment
Share on other sites

Link to post
Share on other sites

unsigned int does not have negative numbers

I haven't test it, but converting -3 into unsigned int will be 3

 

is -3 bigger than 35? false

is 3 bigger than 35? false

Link to comment
Share on other sites

Link to post
Share on other sites

3 minutes ago, zMeul said:

unsigned int does not have negative numbers

I haven't test it, but converting -3 into unsigned int will be 3

 

is -3 bigger than 35? false

is 3 bigger than 35? false

No. It uses 2's compliment to compute the positive number resulting in what is effectively UINT_MAX +1 - Your_Number.

Main Gaming PC - i9 10850k @ 5GHz - EVGA XC Ultra 2080ti with Heatkiller 4 - Asrock Z490 Taichi - Corsair H115i - 32GB GSkill Ripjaws V 3600 CL16 OC'd to 3733 - HX850i - Samsung NVME 256GB SSD - Samsung 3.2TB PCIe 8x Enterprise NVMe - Toshiba 3TB 7200RPM HD - Lian Li Air

 

Proxmox Server - i7 8700k @ 4.5Ghz - 32GB EVGA 3000 CL15 OC'd to 3200 - Asus Strix Z370-E Gaming - Oracle F80 800GB Enterprise SSD, LSI SAS running 3 4TB and 2 6TB (Both Raid Z0), Samsung 840Pro 120GB - Phanteks Enthoo Pro

 

Super Server - i9 7980Xe @ 4.5GHz - 64GB 3200MHz Cl16 - Asrock X299 Professional - Nvidia Telsa K20 -Sandisk 512GB Enterprise SATA SSD, 128GB Seagate SATA SSD, 1.5TB WD Green (Over 9 years of power on time) - Phanteks Enthoo Pro 2

 

Laptop - 2019 Macbook Pro 16" - i7 - 16GB - 512GB - 5500M 8GB - Thermal Pads and Graphite Tape modded

 

Smart Phones - iPhone X - 64GB, AT&T, iOS 13.3 iPhone 6 : 16gb, AT&T, iOS 12 iPhone 4 : 16gb, AT&T Go Phone, iOS 7.1.1 Jailbroken. iPhone 3G : 8gb, AT&T Go Phone, iOS 4.2.1 Jailbroken.

 

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, Hunter259 said:

No. It uses 2's compliment to compute the positive number resulting in what is effectively UINT_MAX +1 - Your_Number.

sorry, I have't tested it and don't a C compiler at hand

Link to comment
Share on other sites

Link to post
Share on other sites

36 minutes ago, CJPowell27 said:

hey guys, I've been trying to figure out signed and unsigned integer casting in C lately and I'm kinda stuck.  If i cast a number like -3 as unsigned, what would it turn out to be? I'm trying to evaluate whether (unsigned) -3 > 35 is true or false and I just can't figure out the casting. Any help is appreciated, thanks.

Compile a program with this code 

int32_t number = - 3;

printf("unsigned %i equals %u", number,  (uint32_t) number);

 

 

That should tell you what it is. You're forcing it to treat the signed integer as an unsigned integer. You can change number to whatever you want, as long as it's not more than 32 bits. 

Make sure to quote or tag me (@JoostinOnline) or I won't see your response!

PSU Tier List  |  The Real Reason Delidding Improves Temperatures"2K" does not mean 2560×1440 

Link to comment
Share on other sites

Link to post
Share on other sites

10 hours ago, CJPowell27 said:

hey guys, I've been trying to figure out signed and unsigned integer casting in C lately and I'm kinda stuck.  If i cast a number like -3 as unsigned, what would it turn out to be? I'm trying to evaluate whether (unsigned) -3 > 35 is true or false and I just can't figure out the casting. Any help is appreciated, thanks.

If you type cast an signed value to unsigned, it becomes the data type's max value plus the value of the signed number. For example, if -3 was an 8-bit data type, its unsigned equivalent would be 252. So (unsigned) -3 > 35 would be true.

 

I would suggest reading this topic because this is how negative numbers for integers are coded: https://en.wikipedia.org/wiki/Two's_complement

Link to comment
Share on other sites

Link to post
Share on other sites

It is worth noting that this signed-unsigned conversion can happen implicitly, for example:

int x = -3;
unsigned int y = 35;

if (x > y)
{
	//will be true!
}

The C standard states that x will implicitly be converted to unsigned before the comparison is made. This is the reason many compilers warn about signed-unsigned comparisons.

 

Also semi-related is the fact that unsigned overflow is defined, but signed overflow is undefined behavior:

unsigned int x = UINT_MAX;
++x;	//Well defined, x == 0

int y = INT_MAX;
++y; 	//Undefined! 

 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×