{"action":"created","issue":{"url":"https://api.github.com/repos/earlymeme/zjc.github.io/issues/7","repository_url":"https://api.github.com/repos/earlymeme/zjc.github.io","labels_url":"https://api.github.com/repos/earlymeme/zjc.github.io/issues/7/labels{/name}","comments_url":"https://api.github.com/repos/earlymeme/zjc.github.io/issues/7/comments","events_url":"https://api.github.com/repos/earlymeme/zjc.github.io/issues/7/events","html_url":"https://github.com/earlymeme/zjc.github.io/issues/7","id":229232094,"number":7,"title":"位操作","user":{"login":"earlymeme","id":1091500,"avatar_url":"https://avatars2.githubusercontent.com/u/1091500?v=3","gravatar_id":"","url":"https://api.github.com/users/earlymeme","html_url":"https://github.com/earlymeme","followers_url":"https://api.github.com/users/earlymeme/followers","following_url":"https://api.github.com/users/earlymeme/following{/other_user}","gists_url":"https://api.github.com/users/earlymeme/gists{/gist_id}","starred_url":"https://api.github.com/users/earlymeme/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/earlymeme/subscriptions","organizations_url":"https://api.github.com/users/earlymeme/orgs","repos_url":"https://api.github.com/users/earlymeme/repos","events_url":"https://api.github.com/users/earlymeme/events{/privacy}","received_events_url":"https://api.github.com/users/earlymeme/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":3,"created_at":"2017-05-17T04:07:34Z","updated_at":"2017-05-25T06:46:48Z","closed_at":null,"body":"### 掩码\r\n掩码可以用来清除某些位，保留某些位。\r\n1. AND运算用来提取位的子集\r\n2. OR运算用来设置某些位\r\n3. 异或运算用来转换某些位（1<->0）\r\n\r\n比如：\r\n```\r\nMask:   00001111b\r\nValue:  01010101b\r\n```\r\n我们想要清除高4位，保留低4位\r\n得到：\r\n```\r\nMask:   00001111b\r\nValue:  01010101b\r\nResult: 00000101b\r\n```\r\n使用AND运算来做。\r\n```\r\nuint8_t stuff(...) {\r\n  uint8_t mask = 0x0f;   // 00001111b\r\n  uint8_t value = 0x55;  // 01010101b\r\n  return mask & value;\r\n}\r\n```\r\n\r\n更普遍的用法是：\r\n从一个很大的字提取单个的字节。我们定义第一个字节为高位。使用 & 和 >> 这2个操作符。\r\n```\r\nvoid more_stuff(uint32_t value) {             // Example value: 0x01020304\r\n    uint32_t byte1 = (value >> 24);           // 0x01020304 >> 24 is 0x01 so\r\n                                              // no masking is necessary\r\n    uint32_t byte2 = (value >> 16) & 0xff;    // 0x01020304 >> 16 is 0x0102 so\r\n                                              // we must mask to get 0x02\r\n    uint32_t byte3 = (value >> 8)  & 0xff;    // 0x01020304 >> 8 is 0x010203 so\r\n                                              // we must mask to get 0x03\r\n    uint32_t byte4 = value & 0xff;            // here we only mask, no shifting\r\n                                              // is necessary\r\n    ...\r\n}\r\n```\r\n也可以先使用不同的掩码完成。\r\n```\r\nuint32_t byte3 = (value & 0xff00) >> 8;\r\n```\r\n\r\nrefs: http://stackoverflow.com/questions/10493411/what-is-bit-masking"},"comment":{"url":"https://api.github.com/repos/earlymeme/zjc.github.io/issues/comments/303936871","html_url":"https://github.com/earlymeme/zjc.github.io/issues/7#issuecomment-303936871","issue_url":"https://api.github.com/repos/earlymeme/zjc.github.io/issues/7","id":303936871,"user":{"login":"earlymeme","id":1091500,"avatar_url":"https://avatars2.githubusercontent.com/u/1091500?v=3","gravatar_id":"","url":"https://api.github.com/users/earlymeme","html_url":"https://github.com/earlymeme","followers_url":"https://api.github.com/users/earlymeme/followers","following_url":"https://api.github.com/users/earlymeme/following{/other_user}","gists_url":"https://api.github.com/users/earlymeme/gists{/gist_id}","starred_url":"https://api.github.com/users/earlymeme/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/earlymeme/subscriptions","organizations_url":"https://api.github.com/users/earlymeme/orgs","repos_url":"https://api.github.com/users/earlymeme/repos","events_url":"https://api.github.com/users/earlymeme/events{/privacy}","received_events_url":"https://api.github.com/users/earlymeme/received_events","type":"User","site_admin":false},"created_at":"2017-05-25T06:46:48Z","updated_at":"2017-05-25T06:46:48Z","body":"```\r\n// roundDownToPowerOfTwo\r\nunsigned flp2(unsigned x) {\r\n  x = x | (x >> 1);\r\n  x = x | (x >> 2);\r\n  x = x | (x >> 4);\r\n  x = x | (x >> 8);\r\n  x = x | (x >> 16);\r\n  return x - (x >> 1);\r\n}\r\n// roundUpToPowerOfTwo\r\nunsigned clp2(unsigned x) {\r\n  x = x − 1;\r\n  x = x | (x >> 1);\r\n  x = x | (x >> 2);\r\n  x = x | (x >> 4);\r\n  x = x | (x >> 8);\r\n  x = x | (x >> 16);\r\n  return x + 1;\r\n}\r\n```\r\nRef： Hacker's Delight figure 3-3\r\n\r\n"}}
{"actor":{"display_login":"earlymeme"}}