{"id":493,"date":"2013-06-18T11:50:45","date_gmt":"2013-06-18T18:50:45","guid":{"rendered":"http:\/\/192.168.1.2\/wordpress\/?p=493"},"modified":"2013-07-26T14:48:38","modified_gmt":"2013-07-26T21:48:38","slug":"ctci-ch-5-1","status":"publish","type":"post","link":"http:\/\/cywang.no-ip.org\/wordpress\/?p=493","title":{"rendered":"CTCI: Ch. 5-1"},"content":{"rendered":"<p>\n\t<strong>You are given two 32-bit numbers, N and M, and two bit positions, i and j. Write a method to insert M into N such that M starts at bit j and ends at bit i.<br \/>\n\tYou can assume that the bits j through I have enough space to fit all of M. That is, if M = 10011, you can assume that there are at least 5 bits between j and i.<br \/>\n\tYou would not, for example, have j = 3 and i = 2, because M could not fully fit between bit 3 and bit 2.<\/strong><br \/>\n\t&nbsp;\n<\/p>\n<p>\n\t<!--more-->\n<\/p>\n<p>\n\t<strong>EXAMPLE<br \/>\n\tInput: N = 10000000000, M = 10011, i = 2, j = 6<br \/>\n\tOutput: N = 10001001100<\/strong>\n<\/p>\n<pre class=\"brush:cpp;\">\r\nunsigned int mergeTwoInt(unsigned int n, unsigned int m, unsigned int i, unsigned int j)\r\n{\r\n    \/\/Step 1. Clear the bits j through i in n\r\n    unsigned int ones = ~0;\r\n    unsigned int left = ones &lt;&lt; (j+1);\r\n    unsigned int right = (1&lt;&lt;i)-1;\r\n    unsigned int mask = left | right;\r\n    \r\n    \/\/Step 2. Shift m, and then merge m and n\r\n    return (n &amp; mask) | (m &lt;&lt; i);\r\n}<\/pre>\n<p>\n\t&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>You are given two 32-bit numbers, N and M, and two bit positions, i and j. Write a method to insert M into N such that M starts at bit j and ends at bit i. You can assume that &hellip; <a href=\"http:\/\/cywang.no-ip.org\/wordpress\/?p=493\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"footnotes":""},"categories":[23,6],"tags":[],"class_list":["post-493","post","type-post","status-publish","format-standard","hentry","category-chapter-5-bit-manipulation","category-cracking-the-coding-interview"],"_links":{"self":[{"href":"http:\/\/cywang.no-ip.org\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/493","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/cywang.no-ip.org\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/cywang.no-ip.org\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/cywang.no-ip.org\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/cywang.no-ip.org\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=493"}],"version-history":[{"count":6,"href":"http:\/\/cywang.no-ip.org\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/493\/revisions"}],"predecessor-version":[{"id":519,"href":"http:\/\/cywang.no-ip.org\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/493\/revisions\/519"}],"wp:attachment":[{"href":"http:\/\/cywang.no-ip.org\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=493"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/cywang.no-ip.org\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=493"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/cywang.no-ip.org\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=493"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}