{"id":28974,"date":"2023-01-04T17:58:20","date_gmt":"2023-01-04T12:28:20","guid":{"rendered":"https:\/\/jassweb.com\/solved\/solved-what-happens-when-bitwise-and-is-applied-to-negative-numbers\/"},"modified":"2023-01-04T17:58:20","modified_gmt":"2023-01-04T12:28:20","slug":"solved-what-happens-when-bitwise-and-is-applied-to-negative-numbers","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-what-happens-when-bitwise-and-is-applied-to-negative-numbers\/","title":{"rendered":"[Solved] What happens when bitwise AND is applied to negative numbers?"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div id=\"answer-56324423\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"56324423\" data-parentid=\"56323845\" data-score=\"6\" data-position-on-page=\"1\" data-highest-scored=\"1\" data-question-has-accepted-highest-score=\"1\" itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n<div class=\"post-layout\">\n<div class=\"votecell post-layout--left\"><\/div>\n<div class=\"answercell post-layout--right\">\n<div class=\"s-prose js-post-body\" itemprop=\"text\">\n<p>In C, in the result of the binary <code>&amp;<\/code>, each bit depends on the two corresponding bits in the operands. If the bit in the same position is set (1) in both operands, it is set in the result. If it is clear (0) in either operand, it is clear in the result. For example, given bits 0011 and 0101, the <code>&amp;<\/code> operator would produce 0001, because only in the last position is the bit set in both operands.<\/p>\n<p>You likely already know that positive integers are represented with binary. The bit positions are numbered starting from 0 on the \u201cright\u201d, then 1 for the next position, 2, 3, and so on. The bit in position <em>i<\/em> represents a value of 2<sup><em>i<\/em><\/sup>, so bit 0 represents 1, bit 1 represents 2, bit 2 represents 4, bit 3 represents 8, bit 4 represents 16, and so on. The value represented by all the bits is the sum of the values of the bits that are set to 1. So 101 represents 5, because the bits for 2<sup>2<\/sup> = 4 and 2<sup>0<\/sup> = 1 are set, and 4+1 = 5.<\/p>\n<p>The C standard specifies three rules that a C implementation may use to represent negative numbers (in C 2018 6.2.6.2 2):<\/p>\n<ul>\n<li>One of the bits represents a sign. If the sign bit is 0, the value is the same as above. If the sign bit is 1, the value is negated. So, if the first bit is the sign bit, then 5 is 0101 is 5, and \u22125 is 1101. This is called <em>sign and magnitude<\/em>.<\/li>\n<li>One of the bits represents a sign, and, if a number is negative, all of the bits are inverted. So 5 is 0101, and \u22125 is 1010. This is called <em>one\u2019s complement<\/em>.<\/li>\n<li>One of the bits represents a sign, and, if the number is negative (let\u2019s call it <em>x<\/em>), the bits are set in the pattern that would be used for 2<sup><em>N<\/em><\/sup>\u2212<em>x<\/em>, where <em>N<\/em> is the number of bits. For example, for four bits, 2<sup><em>N<\/em><\/sup> = 16, and 5 is 0101, and \u22125 is represented with the bits for 16\u22125 = 11, which are 1011. This is called <em>two\u2019s complement<\/em>.<\/li>\n<\/ul>\n<p>In early computer hardware and software, all of the above were tried. The last, two\u2019s complement, is overwhelming dominant in modern computing for integers. (Most floating-point uses sign and magnitude.) Nonetheless, the C standard still permits implementations to use any of the methods.<\/p>\n<p>Because of this, the result of <code>-10 &amp; 5<\/code> is implementation-dependent. I will illustrate using eight bits, with a space to group them into two sets of four bits for visibility:<\/p>\n<p>With two\u2019s complement:<\/p>\n<ul>\n<li>\u221210 is represented with 1111 0110 (256 \u2212 10 = 246 = 128+64+32+16+4+2), 5 uses 0000 0101, and <code>\u221210 &amp; 5<\/code> is 0000 0100, which represents 4.<\/li>\n<\/ul>\n<p>With one\u2019s complement:<\/p>\n<ul>\n<li>\u221210 is represented with 1111 0101, 5 uses 0000 0101, and <code>\u221210 &amp; 5<\/code> is 0000 0101, which represents 5.<\/li>\n<\/ul>\n<p>With sign and magnitude:<\/p>\n<ul>\n<li>\u221210 is represented with 1000 1010, 5 uses 0000 0101, and <code>-10 &amp; 5<\/code> is 0000 0000, which represents 0.<\/li>\n<\/ul>\n<p>Thus, a C implementation that conforms to the C standard could produce 0, 4, or 5 for <code>-10 &amp; 5<\/code>, but 4 is by far the most common result.<\/p>\n<\/p><\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p>            <span class=\"d-none\" itemprop=\"commentCount\">0<\/span> <\/p><\/div>\n<\/div>\n<p>[ad_2]<\/p>\n<p>solved What happens when bitwise AND is applied to negative numbers? <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] In C, in the result of the binary &amp;, each bit depends on the two corresponding bits in the operands. If the bit in the same position is set (1) in both operands, it is set in the result. If it is clear (0) in either operand, it is clear in the result. For &#8230; <a title=\"[Solved] What happens when bitwise AND is applied to negative numbers?\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-what-happens-when-bitwise-and-is-applied-to-negative-numbers\/\" aria-label=\"More on [Solved] What happens when bitwise AND is applied to negative numbers?\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[320],"tags":[1013,2907,324,1774,369],"class_list":["post-28974","post","type-post","status-publish","format-standard","hentry","category-solved","tag-bit-manipulation","tag-bitwise-operators","tag-c","tag-negative-number","tag-operators"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>[Solved] What happens when bitwise AND is applied to negative numbers? - JassWeb<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jassweb.com\/solved\/solved-what-happens-when-bitwise-and-is-applied-to-negative-numbers\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Solved] What happens when bitwise AND is applied to negative numbers? - JassWeb\" \/>\n<meta property=\"og:description\" content=\"[ad_1] In C, in the result of the binary &amp;, each bit depends on the two corresponding bits in the operands. If the bit in the same position is set (1) in both operands, it is set in the result. If it is clear (0) in either operand, it is clear in the result. For ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-what-happens-when-bitwise-and-is-applied-to-negative-numbers\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2023-01-04T12:28:20+00:00\" \/>\n<meta name=\"author\" content=\"Kirat\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Kirat\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-what-happens-when-bitwise-and-is-applied-to-negative-numbers\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-what-happens-when-bitwise-and-is-applied-to-negative-numbers\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"[Solved] What happens when bitwise AND is applied to negative numbers?\",\"datePublished\":\"2023-01-04T12:28:20+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-what-happens-when-bitwise-and-is-applied-to-negative-numbers\/\"},\"wordCount\":466,\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"keywords\":[\"bit-manipulation\",\"bitwise-operators\",\"c++\",\"negative-number\",\"operators\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-what-happens-when-bitwise-and-is-applied-to-negative-numbers\/\",\"url\":\"https:\/\/jassweb.com\/solved\/solved-what-happens-when-bitwise-and-is-applied-to-negative-numbers\/\",\"name\":\"[Solved] What happens when bitwise AND is applied to negative numbers? - JassWeb\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#website\"},\"datePublished\":\"2023-01-04T12:28:20+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-what-happens-when-bitwise-and-is-applied-to-negative-numbers\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/jassweb.com\/solved\/solved-what-happens-when-bitwise-and-is-applied-to-negative-numbers\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-what-happens-when-bitwise-and-is-applied-to-negative-numbers\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/jassweb.com\/solved\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Solved] What happens when bitwise AND is applied to negative numbers?\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/jassweb.com\/solved\/#website\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"name\":\"JassWeb\",\"description\":\"Build High-quality Websites\",\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/jassweb.com\/solved\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\",\"name\":\"Jass Web\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"contentUrl\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"width\":693,\"height\":132,\"caption\":\"Jass Web\"},\"image\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\",\"name\":\"Kirat\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750\",\"contentUrl\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750\",\"caption\":\"Kirat\"},\"sameAs\":[\"http:\/\/jassweb.com\"],\"url\":\"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"[Solved] What happens when bitwise AND is applied to negative numbers? - JassWeb","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/jassweb.com\/solved\/solved-what-happens-when-bitwise-and-is-applied-to-negative-numbers\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] What happens when bitwise AND is applied to negative numbers? - JassWeb","og_description":"[ad_1] In C, in the result of the binary &amp;, each bit depends on the two corresponding bits in the operands. If the bit in the same position is set (1) in both operands, it is set in the result. If it is clear (0) in either operand, it is clear in the result. For ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-what-happens-when-bitwise-and-is-applied-to-negative-numbers\/","og_site_name":"JassWeb","article_published_time":"2023-01-04T12:28:20+00:00","author":"Kirat","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kirat","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jassweb.com\/solved\/solved-what-happens-when-bitwise-and-is-applied-to-negative-numbers\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-what-happens-when-bitwise-and-is-applied-to-negative-numbers\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"[Solved] What happens when bitwise AND is applied to negative numbers?","datePublished":"2023-01-04T12:28:20+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-what-happens-when-bitwise-and-is-applied-to-negative-numbers\/"},"wordCount":466,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"keywords":["bit-manipulation","bitwise-operators","c++","negative-number","operators"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-what-happens-when-bitwise-and-is-applied-to-negative-numbers\/","url":"https:\/\/jassweb.com\/solved\/solved-what-happens-when-bitwise-and-is-applied-to-negative-numbers\/","name":"[Solved] What happens when bitwise AND is applied to negative numbers? - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"datePublished":"2023-01-04T12:28:20+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-what-happens-when-bitwise-and-is-applied-to-negative-numbers\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-what-happens-when-bitwise-and-is-applied-to-negative-numbers\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-what-happens-when-bitwise-and-is-applied-to-negative-numbers\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"[Solved] What happens when bitwise AND is applied to negative numbers?"}]},{"@type":"WebSite","@id":"https:\/\/jassweb.com\/solved\/#website","url":"https:\/\/jassweb.com\/solved\/","name":"JassWeb","description":"Build High-quality Websites","publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jassweb.com\/solved\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/jassweb.com\/solved\/#organization","name":"Jass Web","url":"https:\/\/jassweb.com\/solved\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/","url":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","contentUrl":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","width":693,"height":132,"caption":"Jass Web"},"image":{"@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31","name":"Kirat","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/","url":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750","caption":"Kirat"},"sameAs":["http:\/\/jassweb.com"],"url":"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/"}]}},"_links":{"self":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/28974","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/comments?post=28974"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/28974\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=28974"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=28974"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=28974"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}