Skip to content

Latest commit

 

History

History
17 lines (13 loc) · 761 Bytes

File metadata and controls

17 lines (13 loc) · 761 Bytes

Programming Micro Note 20161017

Negative number assigned to unsigned

Source:
What happens if I assign a negative value to an unsigned variable? Q:
What happes if I do

unsigned int n = -1;

A:

Section 4.7 If the destination type is unsigned, the resulting value is the least unsigned integer congruent to the source integer (modulo 2^n where n is the number of bits used to represent the unsigned type). [ Note: In a two’s complement representation, this conversion is conceptual and there is no change in the bit pattern (if there is no truncation)

so it's 0xFFFFFFFF (on a machine defining unsigned as 32 bits);