c - Signed to unsigned with subtraction -


i've decided make leap , read through bunch of computer science books better equip myself future.

at moment i'm reading through converting signed unsigned decimals. understand majority if (hopefully becomes easier eventually), struggling following (in 32-bit):

-2147483647-1u < -2147483647

according book, evaluates true. there's bit i'm still struggling cant see why evaluates this.

with understanding, know both converted unsigned values in calculation due first number being cast unsigned. first number therefore -2147483648 after subtraction , converted unsigned, or unsigned conversion happen prior subtraction?

sorry lengthy post, trying head around understanding this.

thanks!

the first number therefore -2147483648 after subtraction , converted unsigned, or unsigned conversion happen prior subtraction?

the latter true. according c11 standard "6.3.1.8 usual arithmetic conversions":

...otherwise, if operand has unsigned integer type has rank greater or equal rank of type of other operand, operand signed integer type converted type of operand unsigned integer type.

so, all of operands, both ones subtraction , comparison converted unsigned.

p.s. completeness, conversion procedure described in "6.3.1.3 signed , unsigned integers" :

... if new type unsigned, value converted repeatedly adding or subtracting 1 more maximum value can represented in new type until value in range of new type.

p.p.s. answer assuming int being 32-bit long. if less that, negative constant -2147483647 have type long higher rank unsigned int, 1u , above not apply , no conversion performed (thanks @olaf pointing out).


Comments

Popular posts from this blog

ios - MKAnnotationView layer is not of expected type: MKLayer -

ZeroMQ on Windows, with Qt Creator -

unity3d - Unity SceneManager.LoadScene quits application -