{"id":5096,"date":"2020-01-31T13:20:37","date_gmt":"2020-01-31T13:20:37","guid":{"rendered":"http:\/\/www.max-sperling.bplaced.net\/?p=5096"},"modified":"2025-03-19T14:18:07","modified_gmt":"2025-03-19T14:18:07","slug":"integer-overflow","status":"publish","type":"post","link":"http:\/\/www.max-sperling.bplaced.net\/?p=5096","title":{"rendered":"Integer overflow (C++)"},"content":{"rendered":"<p><strong>Unsigned integer<\/strong><\/p>\n<pre>\r\nRange: [0,255]\r\nBinary:   00000000 =++=> 00000001 =++=> ... =++=> 11111111\r\nHex:      0x00     =++=> 0x01     =++=> ... =++=> 0xFF\r\nInterpr.: 0        =++=> 1        =++=> ... =++=> 255\r\n<\/pre>\n<p>&#8220;Unsigned integers shall obey the laws of arithmetic modulo 2^n where n is the number of bits in the value representation of that particular size of integer.&#8221; (C++ Standard (N4713) &#8211; 6.7.1.4)<\/p>\n<pre class=\"brush: cpp; gutter: false; title: ; notranslate\" title=\"\">\r\n#include &lt;stdint.h&gt;\r\n\r\nint main()\r\n{\r\n  uint8_t i = 255;\r\n  i++; \/\/ definded behavior, i = 0 (= 256 % 2^8)\r\n  return 0;\r\n}\r\n<\/pre>\n<hr>\n<p><strong>Signed integer<\/strong><br \/>\n<u>Signed Magnitude<\/u> (old processors)<\/p>\n<pre>\r\nRange: [-127,127]\r\nBinary:   00000000 =++=> ... =++=> 01111111 =++=> 10000000 =++=> ... =++=> 11111111\r\nHex:      0x00     =++=> ... =++=> 0x7F     =++=> 0x80     =++=> ... =++=> 0XFF\r\nInterpr.: +0       =++=> ... =++=> +127     =++=> -0       =++=> ... =++=> -127\r\n<\/pre>\n<p><u>One&#8217;s complement<\/u> (old processors)<\/p>\n<pre>\r\nRange: [-127,127]\r\nBinary:   00000000 =++=> ... =++=> 01111111 =++=> 10000000 =++=> ... =++=> 11111111\r\nHex:      0x00     =++=> ... =++=> 0x7F     =++=> 0x80     =++=> ... =++=> 0XFF\r\nInterpr.: +0       =++=> ... =++=> +127     =++=> -127     =++=> ... =++=> -0\r\n<\/pre>\n<p><u>Two&#8217;s complement<\/u> (new processors)<br \/>\n&#8211;> Advantage: Basic arithmetic operations are identical to the unsigned ones.<\/p>\n<pre>\r\nRange: [-128,127]\r\nBinary:   00000000 =++=> ... =++=> 01111111 =++=> 10000000 =++=> ... =++=> 11111111\r\nHex:      0x00     =++=> ... =++=> 0x7F     =++=> 0x80     =++=> ... =++=> 0XFF\r\nInterpr.: +\/-0     =++=> ... =++=> +127     =++=> -128     =++=> ... =++=> -1\r\n<\/pre>\n<p>&#8220;If during the evaluation of an expression, the result is not mathematically defined or not in the range of representable values for its type, the behavior is undefined.&#8221; (C++ Standard (N4713) &#8211; 8.1.4)<\/p>\n<pre class=\"brush: cpp; gutter: false; title: ; notranslate\" title=\"\">\r\n#include &lt;stdint.h&gt;\r\n\r\nint main()\r\n{\r\n  int8_t i = 127;\r\n  i++; \/\/ undefined behavior, but often i = -128 (2&#039;s comp.)\r\n  return 0;\r\n}\r\n<\/pre>\n<hr>\n<p><strong>References<\/strong><br \/>\n<a href=\"http:\/\/www.open-std.org\/jtc1\/sc22\/wg21\/docs\/papers\/2017\/n4713.pdf\">C++ Standard (N4713)<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Unsigned integer Range: [0,255] Binary: 00000000 =++=> 00000001 =++=> &#8230; =++=> 11111111 Hex: 0x00 =++=> 0x01 =++=> &#8230; =++=> 0xFF<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false},"categories":[80],"tags":[],"_links":{"self":[{"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/posts\/5096"}],"collection":[{"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5096"}],"version-history":[{"count":4,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/posts\/5096\/revisions"}],"predecessor-version":[{"id":18567,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/posts\/5096\/revisions\/18567"}],"wp:attachment":[{"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5096"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5096"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5096"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}