{"id":1057,"date":"2023-05-02T20:56:59","date_gmt":"2023-05-02T19:56:59","guid":{"rendered":"https:\/\/www.kolkataonweb.com\/code-bank\/?p=1057"},"modified":"2023-05-02T21:35:48","modified_gmt":"2023-05-02T20:35:48","slug":"unsigned-int-mathematical-operations-and-pitfalls-in-arduino-or-c","status":"publish","type":"post","link":"https:\/\/www.kolkataonweb.com\/code-bank\/arduino\/unsigned-int-mathematical-operations-and-pitfalls-in-arduino-or-c\/","title":{"rendered":"Unsigned Int,  Mathematical operations and pitfalls in Arduino or C"},"content":{"rendered":"\n<p>Let&#8217;s consider the below code:<\/p>\n\n\n\n<pre class='wp-code-highlight prettyprint'><br>unsigned long startTime = 1940, stopTime = 0;<br><br>if( (stopTime - startTime) &gt; 5000ul)<br>{<br>  Serial.println(\"Hello World!\");<br>}<br><br><\/pre>\n\n\n\n<p>Will this say Hello World? <\/p>\n\n\n\n<p>Yes, it will. If this sounds new or surprising to you, please continue reading below.<\/p>\n\n\n\n<p>Note the variables have been defined as unsigned long. So the result of mathematical operations will also be in unsigned value. Hence though the result of the operation apparently looks to be -1940 but in practise  it will be 4294965356<\/p>\n\n\n\n<p>And thus the comparison in the if() will evaluate to true. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Let&#8217;s consider the below code: unsigned long startTime = 1940, stopTime = 0;if( (stopTime &#8211; startTime) &gt; 5000ul){ Serial.println(&#8220;Hello World!&#8221;);} Will this say Hello World? Yes, it will. If this sounds new or surprising to you, please continue reading below. Note the variables have been defined as unsigned long. So the result of mathematical operations&hellip; <a class=\"more-link\" href=\"https:\/\/www.kolkataonweb.com\/code-bank\/arduino\/unsigned-int-mathematical-operations-and-pitfalls-in-arduino-or-c\/\">Continue reading <span class=\"screen-reader-text\">Unsigned Int,  Mathematical operations and pitfalls in Arduino or C<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10],"tags":[13,406,407,405],"class_list":["post-1057","post","type-post","status-publish","format-standard","hentry","category-arduino","tag-arduino","tag-arduino-millis","tag-arduino-unsigned-long-operations","tag-unsigned-long","entry"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.kolkataonweb.com\/code-bank\/wp-json\/wp\/v2\/posts\/1057","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.kolkataonweb.com\/code-bank\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.kolkataonweb.com\/code-bank\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.kolkataonweb.com\/code-bank\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.kolkataonweb.com\/code-bank\/wp-json\/wp\/v2\/comments?post=1057"}],"version-history":[{"count":3,"href":"https:\/\/www.kolkataonweb.com\/code-bank\/wp-json\/wp\/v2\/posts\/1057\/revisions"}],"predecessor-version":[{"id":1060,"href":"https:\/\/www.kolkataonweb.com\/code-bank\/wp-json\/wp\/v2\/posts\/1057\/revisions\/1060"}],"wp:attachment":[{"href":"https:\/\/www.kolkataonweb.com\/code-bank\/wp-json\/wp\/v2\/media?parent=1057"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kolkataonweb.com\/code-bank\/wp-json\/wp\/v2\/categories?post=1057"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kolkataonweb.com\/code-bank\/wp-json\/wp\/v2\/tags?post=1057"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}