{"id":629,"date":"2022-08-30T15:04:32","date_gmt":"2022-08-30T15:04:32","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/09\/float-bits-and-strict-aliasing-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:04:32","modified_gmt":"2022-08-30T15:04:32","slug":"float-bits-and-strict-aliasing-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/float-bits-and-strict-aliasing-collection-of-common-programming-errors\/","title":{"rendered":"float bits and strict aliasing-Collection of common programming errors"},"content":{"rendered":"<p>I am trying to extract the bits from a float without invoking undefined behavior. Here is my first attempt:<\/p>\n<pre><code>unsigned foo(float x)\n{\n    unsigned* u = (unsigned*)&amp;x;\n    return *u;\n}\n<\/code><\/pre>\n<p>As I understand it, this is not guaranteed to work due to strict aliasing rules, right? Does it work if a take an intermediate step with a character pointer?<\/p>\n<pre><code>unsigned bar(float x)\n{\n    char* c = (char*)&amp;x;\n    unsigned* u = (unsigned*)c;\n    return *u;\n}\n<\/code><\/pre>\n<p>Or do I have to extract the individual bytes myself?<\/p>\n<pre><code>unsigned baz(float x)\n{\n    unsigned char* c = (unsigned char*)&amp;x;\n    return c[0] | c[1]<\/code><\/pre>\n<p id=\"rop\"><small>Originally posted 2013-11-09 21:08:56. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>I am trying to extract the bits from a float without invoking undefined behavior. Here is my first attempt: unsigned foo(float x) { unsigned* u = (unsigned*)&amp;x; return *u; } As I understand it, this is not guaranteed to work due to strict aliasing rules, right? Does it work if a take an intermediate step [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-629","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/629","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=629"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/629\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=629"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=629"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=629"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}