{"id":6173,"date":"2023-12-30T00:01:37","date_gmt":"2023-12-30T08:01:37","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=6173"},"modified":"2024-01-06T09:51:24","modified_gmt":"2024-01-06T17:51:24","slug":"binary-notation-in-c23","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=6173","title":{"rendered":"Binary Notation in C23"},"content":{"rendered":"<p> While the C language offers hexadecimal notation and output, it ignores binary. Now with the C 23 standard, binary expression and output is supported.<br \/>\n<!--more--><br \/>\nIf you were to guess, what do you think would be the prefix for an integer expressed in binary notation?<\/p>\n<p>Yep, it&#8217;s <code>0b<\/code>. Simple. Consistent.<\/p>\n<p>The value 26 expressed in binary for the C23 standard looks like this: <code>0b11010<\/code><\/p>\n<p>Here is sample code for C23 that outputs a binary integer value in decimal and hex:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2023_12_30-Lesson-a.c\" rel=\"noopener\" target=\"_blank\">2023_12_30-Lesson-a.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    int a = 0b11010;\r\n\r\n    printf(\"Decimal: %d\\n\",a);\r\n    printf(\"Hex: %x\\n\",a);\r\n\r\n    return 0;\r\n}<\/pre>\n<p>Remember from <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=6152\">last week&#8217;s Lesson<\/a>, you must specify the <code>-std=c2x<\/code> switch for the compiler to recognize and use the C23 standard. When active, the switch enables the compiler to recognize the <code>0b11010<\/code> notation and value. Here is the output:<\/p>\n<p><code>Decimal: 26<br \/>\nHex: 1a<\/code><\/p>\n<p>The C23 flavor has another binary trick up its sleeve: the tick binary separator. The tick separator isn&#8217;t a new C feature. It&#8217;s available now with hexadecimal values. With binary notation, however, the tick separator becomes highly useful.<\/p>\n<p>Programmers often write binary values in clumps of four. These match up with the hex digits 0 through F:<\/p>\n<table>\n<tr>\n<td>Binary<\/td>\n<td>Hex<\/td>\n<td>Binary<\/td>\n<td>Hex<\/td>\n<td>Binary<\/td>\n<td>Hex<\/td>\n<td>Binary<\/td>\n<td>Hex<\/td>\n<\/tr>\n<tr>\n<td>0000<\/td>\n<td>0<\/td>\n<td>0100<\/td>\n<td>4<\/td>\n<td>1000<\/td>\n<td>8<\/td>\n<td>1100<\/td>\n<td>c<\/td>\n<\/tr>\n<tr>\n<td>0001<\/td>\n<td>1<\/td>\n<td>0101<\/td>\n<td>5<\/td>\n<td>1001<\/td>\n<td>9<\/td>\n<td>1101<\/td>\n<td>d<\/td>\n<\/tr>\n<tr>\n<td>0010<\/td>\n<td>2<\/td>\n<td>0110<\/td>\n<td>6<\/td>\n<td>1010<\/td>\n<td>a<\/td>\n<td>1110<\/td>\n<td>e<\/td>\n<\/tr>\n<tr>\n<td>0011<\/td>\n<td>3<\/td>\n<td>0111<\/td>\n<td>7<\/td>\n<td>1011<\/td>\n<td>b<\/td>\n<td>1111<\/td>\n<td>f<\/td>\n<\/tr>\n<\/table>\n<p>I&#8217;ve written about these <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=5675\">tick separators<\/a> before, specifically how they&#8217;re used in hexadecimal values. Here&#8217;s how they&#8217;re useful in binary notation:<\/p>\n<p><code>int a = 0b001'1010;<\/code><\/p>\n<p>The ticks need not be used every four positions, either:<\/p>\n<p><code>int a = 0b0'01'10'10;<\/code><\/p>\n<p>Or even:<\/p>\n<p><code>int a = 0b00'11010;<\/code><\/p>\n<p>How you place the ticks is up to you, but they&#8217;re used to separate a long binary value for easy reading.<\/p>\n<p>The C23 standard also defines the <code>%b<\/code> conversion specification, which is used as a placeholder in a <em>printf()<\/em> or <em>scanf()<\/em> format string to output or accept binary values. So the following statement could be added to the code:<\/p>\n<p><code>printf(\"Binary: %b\\n\",a);<\/code><\/p>\n<p>For some reason, this conversion specification throws a warning when <em>clang-15<\/em> builds the program. Even so, a program is created:<\/p>\n<p><code>Decimal: 26<br \/>\nHex: 1a<br \/>\nBinary: 11010<\/code><\/p>\n<p>I tested the <code>%b<\/code> placeholder with the following code as well:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2023_12_30-Lesson-b.c\" rel=\"noopener\" target=\"_blank\">2023_12_30-Lesson-b.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    int b = 26;\r\n\r\n    printf(\"%b\\n\",b);\r\n\r\n    return 0;\r\n}<\/pre>\n<p>Again, the warning is generated, but the program produces the proper output:<\/p>\n<p><code>11010<\/code><\/p>\n<p>Feeling saucy, I decided to see whether a width specifier works with the <code>%b<\/code> placeholder. I changed the program&#8217;s <em>printf()<\/em> statement to read <code>%8b<\/code>. Here&#8217;s the output:<\/p>\n<p><code>&nbsp;&nbsp;&nbsp;11010<\/code><\/p>\n<p>Yes, the output is 8-characters wide, but I wanted leading zeros. So I updated the code to use <code>%08b<\/code> instead. Here&#8217;s the result:<\/p>\n<p><code>00011010<\/code><\/p>\n<p>Pretty cool, especially to this nerd.<\/p>\n<p>The <code>%b<\/code> placeholder does not work in the <em>scanf()<\/em> function for reading binary input. I figure this feature isn&#8217;t implemented for the <em>clang-15<\/em> compiler I&#8217;m using. My guess and hope is that it finds its way into a future release of <em>clang<\/em>.<\/p>\n<p>In <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=6191\">next week&#8217;s Lesson<\/a> I cover a new C23 data type, precise integers. Happy New Year, everyone!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>One of the great tricks available in the new C23 standard is the capability to express and output binary values. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=6173\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-6173","post","type-post","status-publish","format-standard","hentry","category-main"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6173","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=6173"}],"version-history":[{"count":12,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6173\/revisions"}],"predecessor-version":[{"id":6211,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6173\/revisions\/6211"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6173"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6173"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6173"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}