
{"id":16755,"date":"2017-08-29T14:27:25","date_gmt":"2017-08-29T14:27:25","guid":{"rendered":"http:\/\/www.beautifulwork.org\/?p=16755"},"modified":"2017-08-29T14:27:25","modified_gmt":"2017-08-29T14:27:25","slug":"bit-level-operations-in-c","status":"publish","type":"post","link":"https:\/\/www.trueangle.org\/index.php\/2017\/08\/29\/bit-level-operations-in-c\/","title":{"rendered":"Bit level Operations in C"},"content":{"rendered":"<pre>\nBit-Level Operations in C\n\nTechnical Overview\n\nOne of the reasons why C has become so popular is that it allows the programmer to do many types of machine-level applications without resorting to using machine language as the vehicle for programming. One example of that is C's ability to work with memory addresses. Another example, or class of examples, consists of C constructs for bit-level operations, which will be described here.\n\nAND and OR Operators\n\nC includes the bitwise-AND operator &amp; and the bitwise-OR operator |. To AND two bits together, one merely multiplies them: 0 AND 0 = 0, 1 AND 0 = 0, 0 AND 1 = 0, 1 AND 1 = 1. To OR two bits together, we just add them, except that 1 OR 1 is equal to 1, not 2: 0 OR 0 = 0, 1 OR 0 = 1, 0 or 1 = 1, 1 or 1 = 1.\n\nNote that\n\nAND-ing by a 1 simply copies the bit value\nOR-ing by a 0 simply copies the bit value\nAND-ing by a 0 results in a 0, not matter what the original bit value was\nOR-ing by a 1 results in a 1, not matter what the original bit value was\nThe AND operator is useful for\n\n(a)\nputting 0s in certain bit positions of a variable, while retaining whatever original values were in the other bit positions\n(b)\ntesting whether certain bit positions within a variable contain 1s or 0s\nIn the next few examples, suppose the variable X is of type int.\n\nAs an example of (a), suppose we wish to put 0s in Bit Positions 3 and 2 (the rightmost bit position being Bit Position 0), and suppose we have a machine\/compiler combination which implements int variables as 4-byte quantities (again, this is typical for most workstations today). We would accomplish our goal with the statement\n\nX &amp;= 0xfffffff3;\nHere is why: First, note that\n\nX &amp;= 0xfffffff3\nis just a short form of\n\nX = X &amp; 0xfffffff3\n(like X += 5 vs. X = X + 5). So we are AND-ing 0xfffffff3 with X and putting the result back in X. The constant 0xfffffff3 is, in bit form,\n\n11111111111111111111111111110011\nMost of the bits here are 1s, and since, as mentioned earlier, AND-ing by a 1 produces no change, most bits in X will not change. Only the bits at Bit Positions 3 and 2 will potentially change: They will change to 0s if they were 1s (or stay at 0s if they were 0s).\n\nFor example, after executing the statements\n\nX = 29;\nX &amp;= 0xfffffff3;\nX will have the value 17 (try this yourself on the machine, and make sure you understand).\n\nBy the way, the value to be AND-ed with X, in this case 0xfffffff3, is called a mask. To see why, think of what happens when you wish to paint the walls of a room. You would like to cover up the electrical sockets so that they don't get painted; you can do this with masking tape. Well, in the example above, we wished to ``cover up'' all the bits except those at Bit Positions 3 and 2, so we AND-ed them with 1s, so that they would not change.\n\nAs an example of (b), suppose we wish to set Z = 12 if there is a 1 in Bit Position 5 of X. We could do this with the statement\n\nif (X &amp; 0x00000020) Z = 12;\nHere is why this statement accomplishes this goal: The mask 0x00000020 has 0s everywhere except in Bit Position 5, where there is a 1. Recall from above that AND-ing with a 0 produces a 0, no matter what value it is AND-ed with, while AND-ing with a 1 results in copying the bit value. So the quantity X &amp; 0x00000020 will have 0s everywhere except at Bit Position 5, where there will be a copy of X's original Bit 5. If the latter is a 1, then X &amp; 0x00000020 will be nonzero (0x00000010, to be specific), and since any nonzero value is condidered `true', Z = 12 will be executed.\n\nOR-ing is used to put 1s at specific bit positions. You should verify, and make sure you understand, that after the statements\n\nX = 29;\nX |= 0x00000020;\nare executed, X will be equal to 61.\n\nShift Operators\n\nThe operators &lt;&lt; and &gt;&gt; shift the bits in a variable toward the left or toward the right, respectively. Suppose for example that X and Y are of type int, and we execute\n\nX = 5;\nY = X &lt;&lt; 3;\nThen (say on the CAE machines) X will be, in bit form,\n\n00000000000000000000000000000101\nAll these bits will be shifted left by 3 positions, and the result placed in Y. The latter will now be\n\n00000000000000000000000000101000\nwhich has the value 40.\n\nfootnote: Note that since we are tacking three 0s on the right end, and we are working in base-2, we are in effect multiplying by . (In base-10, tacking on a 0 on the right does a multiplication by 10, e.g. 52 to 520, so in base-2, appending a 0 on the right does a multiplication by 2.) Thus since X was 5, Y will be 40.\nNote that the bits on the left end disappear. This may create problems. For example, shifting may turn a positive number into a negative one, or vice versa, since the leftmost bit of a number tells its sign. This presents no problem, though, for variables of type unsigned, so shift operations are typically done on variables of that type, not of type int.\n\nBit Fields\n\nThese arise in a special type of struct, which allows one to assign variable names to groups of bits within a variable. We will not go into this here.\n\n<a href=\"http:\/\/heather.cs.ucdavis.edu\/~matloff\/UnixAndC\/CLanguage\/BitOps.html\">http:\/\/heather.cs.ucdavis.edu\/~matloff\/UnixAndC\/CLanguage\/BitOps.html<\/a>\n\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Bit-Level Operations in C Technical Overview One of the reasons why C has become so popular is that it allows the programmer to do many types of machine-level applications without resorting to using machine language as the vehicle for programming. One example of that is C&#8217;s ability to work with memory addresses. Another example, or &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.trueangle.org\/index.php\/2017\/08\/29\/bit-level-operations-in-c\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Bit level Operations in C&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[83],"tags":[478],"_links":{"self":[{"href":"https:\/\/www.trueangle.org\/index.php\/wp-json\/wp\/v2\/posts\/16755"}],"collection":[{"href":"https:\/\/www.trueangle.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.trueangle.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.trueangle.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.trueangle.org\/index.php\/wp-json\/wp\/v2\/comments?post=16755"}],"version-history":[{"count":0,"href":"https:\/\/www.trueangle.org\/index.php\/wp-json\/wp\/v2\/posts\/16755\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.trueangle.org\/index.php\/wp-json\/wp\/v2\/media?parent=16755"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.trueangle.org\/index.php\/wp-json\/wp\/v2\/categories?post=16755"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.trueangle.org\/index.php\/wp-json\/wp\/v2\/tags?post=16755"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}