{"id":5464,"date":"2013-05-03T00:00:15","date_gmt":"2013-05-03T00:00:15","guid":{"rendered":"http:\/\/craftydba.com\/?p=5464"},"modified":"2017-10-08T15:59:12","modified_gmt":"2017-10-08T15:59:12","slug":"bitwise-operators","status":"publish","type":"post","link":"https:\/\/craftydba.com\/?p=5464","title":{"rendered":"Bitwise Operators"},"content":{"rendered":"<p><a href=\"https:\/\/craftydba.com\/wp-content\/uploads\/2013\/05\/logic-symbols-table.bmp\"><img decoding=\"async\" src=\"https:\/\/craftydba.com\/wp-content\/uploads\/2013\/05\/logic-symbols-table.bmp\" alt=\"\" title=\"logic-symbols-table\" class=\"aligncenter size-full wp-image-5468\" \/><\/a><\/p>\n<p>I am going to carry one with my series of very short articles or tidbits on Transaction SQL Operators. An operator is a symbol specifying an action that is performed on one or more expressions. <\/p>\n<p>I will exploring the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms176122(v=sql.90).aspx\">Bitwise Operators<\/a> today.  Every piece of data on a computer is stored as a series of bits, either 1 or 0.  Four bits are grouped together to become what is called a <a href=\"https:\/\/en.wikipedia.org\/wiki\/Nibble\">nibble<\/a>.  Two nibbles are combined to become a byte.  The least significant bits are named  <span style=\"color: #DD0000;\">lower nibble<\/span> and the most significant bits are called  <span style=\"color: #DD0000;\">upper nibble<\/span>.<\/p>\n<p>The truth tables are the basic foundation of <a href=\"http:\/\/www.ee.surrey.ac.uk\/Projects\/Labview\/gatesfunc\/#andgate\">logic gates<\/a>.  These are the gates that go into transistors, the building blocks of computers.<\/p>\n<p>TSQL supports the following three basic bit operations:  <a href=\"https:\/\/en.wikipedia.org\/wiki\/Bitwise_operation#AND\">AND<\/a>, <a href=\"https:\/\/en.wikipedia.org\/wiki\/Bitwise_operation#OR\">OR<\/a>, and <a href=\"https:\/\/en.wikipedia.org\/wiki\/Exclusive_or\">XOR<\/a>.  The example below perform bit-wise calculation using each operator.<\/p>\n<pre class=\"lang:TSQL theme:familiar mark:1,2-3\" title=\"ansi standard - bitwise operators\">\r\n--\r\n--  Bitwise operators\r\n--\r\n\r\n-- Operator & (AND)\r\ndeclare @a1 tinyint = 0xFF;\r\ndeclare @a2 tinyint = 0x02;\r\nselect convert(binary(1), (@a1 & @a2)) as low_nibble_bit_two;\r\n\r\n\r\n-- Operator | (OR)\r\ndeclare @o1 tinyint = 0x00;\r\ndeclare @o2 tinyint = 0x40;\r\nselect convert(binary(1), (@o1 | @o2)) as high_nibble_bit_three;\r\n\r\n\r\n-- Operator | (XOR)\r\ndeclare @x1 tinyint = 0xAA;\r\ndeclare @x2 tinyint = 0x55;\r\nselect convert(binary(1), (@x1 ^ @x2)) as all_bits_on;\r\n<\/pre>\n<\/p>\n<p>Here are the logic rules for bit wise operators:  the AND operator evaluates to 1 if both bits are 1; the OR operator evaluates to 1 if at least one bit is 1; and the XOR operator evaluates to 1 only if one bit is 1.  All other cases evaluate to 0.<\/p>\n<p>The output of each calculation is listed below.<\/p>\n<pre class=\"lang:TSQL theme:epicgeeks\" title=\"output\">\r\n\r\noutput: \r\n\r\nlow_nibble_bit_two\r\n------------------\r\n0x02\r\n\r\nhigh_nibble_bit_three\r\n---------------------\r\n0x40\r\n\r\nall_bits_on\r\n-----------\r\n0xFF\r\n\r\n<\/pre>\n<\/p>\n<p>Next time, I will be reviewing the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms188074.aspx\">Comparison Operators<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I am going to carry one with my series of very short articles or tidbits on Transaction SQL Operators. An operator is a symbol specifying an action that is performed on one or more expressions. I will exploring the Bitwise Operators today. Every piece of data on a computer is stored as a series of bits, either 1 or 0. Four bits are grouped together to become what is called a nibble. Two nibbles are combined to become a byte. The least significant bits are named lower nibble and the&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[814],"tags":[835,31,15,28,29],"class_list":["post-5464","post","type-post","status-publish","format-standard","hentry","category-very-short-articles","tag-bitwise-operator","tag-database-developer","tag-john-f-miner-iii","tag-sql-server","tag-tsql"],"_links":{"self":[{"href":"https:\/\/craftydba.com\/index.php?rest_route=\/wp\/v2\/posts\/5464","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/craftydba.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/craftydba.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/craftydba.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/craftydba.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5464"}],"version-history":[{"count":0,"href":"https:\/\/craftydba.com\/index.php?rest_route=\/wp\/v2\/posts\/5464\/revisions"}],"wp:attachment":[{"href":"https:\/\/craftydba.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5464"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/craftydba.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5464"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/craftydba.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5464"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}