{"id":1220,"date":"2014-08-12T07:53:40","date_gmt":"2014-08-11T23:53:40","guid":{"rendered":"http:\/\/www.clonefactor.com\/wordpress\/?p=1220"},"modified":"2015-06-25T15:39:31","modified_gmt":"2015-06-25T07:39:31","slug":"%e9%97%9c%e6%96%bc-c-warning-cs0675-the-operator-used-on-the-sign-extended","status":"publish","type":"post","link":"https:\/\/www.clonefactor.com\/wordpress\/program\/c\/1220\/","title":{"rendered":"\u95dc\u65bc C# warning CS0675: The operator `|&#8217; used on the sign-extended"},"content":{"rendered":"<p>\u4eca\u5929\u5728\u505a\u4e00\u500b 16 \u9032\u5236 Mask \u7684\u6642\u5019\u51fa\u4e86\u4e00\u500b Warning.<\/p>\n<pre class=\"brush:csharp\">private static int ShiftOverwrite(int original, uint mask, int value, int shift)\r\n{\r\n\tvar maskedOriginal = (original &amp; mask);\r\n\tvar newBits = value &lt;&lt; shift;\r\n\treturn (int)(maskedOriginal | newBits);\r\n}<\/pre>\n<p>Warning \u662f\u9019\u6a23\u00a0warning CS0675: The operator `|&#8217; used on the sign-extended type `int&#8217;. Consider casting to a smaller unsigned type first<\/p>\n<p>\u539f\u56e0\u662f\u5728 (original &amp; mask) \u7684\u6642\u5019, \u6c92\u6709\u505a\u597d casting. original \u662fint , mask \u662f uint.<\/p>\n<p>\u539f\u610f\u662f\u628a\u539f\u672c\u7684\u6578\u503c\u7528 mask \u628a\u60f3\u95dc\u7684\u4f4d\u7f6e\u53d6\u51fa<\/p>\n<p>\u554f\u984c\u662f\u600e\u6a23 casting ? \u4ee5\u4e0b\u662f\u7b54\u6848<\/p>\n<pre class=\"brush:csharp\">private static int ShiftOverwrite(int original, uint mask, int value, int shift)\r\n{\r\n\tvar maskedOriginal = (int)((uint)original &amp; mask);\r\n\tvar newBits = value &lt;&lt; shift;\r\n\treturn (int)(maskedOriginal | newBits);\r\n}<\/pre>\n<p>\u9996\u5148\u8981\u628a\u8f38\u5165\u7684\u6578\u5b57, \u5728\u9019\u88e1\u662f\u7528\u4e00\u500b int ARGB \u7684\u503c\u4f86\u5132\u5b58 4 \u500b channel \u7684\u503c.<\/p>\n<p>ARGB = ShiftOverwrite(ARGB, 0xFF00FFFF, value, 16);<\/p>\n<p>\u90a3\u9ebc<\/p>\n<p>(original &amp; mask) \u5247\u662f (ARGB &amp; 0xFF00FFFF) \u7528\u4f5c\u62bd\u53d6 Red channel \u7684\u503c.<\/p>\n<p>\u6240\u4ee5\u5148\u8981\u628a original \u7531 (int) \u8f49\u70ba (uint), mask \u904e\u4e4b\u5f8c\u518d\u8f49\u70ba (int)<\/p>\n<p>\u5728\u9019\u88e1\u8981\u60e1\u88dc\u4e00\u4e0b uint \u53ca int \u7684\u5206\u5225<\/p>\n<p>\u7c21\u55ae\u7684\u8aaa int \u5c31\u662f interget \u5587,<\/p>\n<p>uint \u5c31\u662f usign interger . \u5373\u662f\u6c92\u6709\u8ca0\u6578\u7684 int<\/p>\n<p>\u4f46\u6700\u91cd\u8981\u7684\u662f\u4ed6\u5011\u7684\u5132\u5b58\u7a7a\u9593\u5927\u5c11, int \u662f 32 bit \u7684.<\/p>\n<p>\u5728 c# \u88e1\u6709 int32 \u7684 type. \u610f\u601d\u5c31\u662f\u4e00\u500b 32 \u4f4d\u9577\u7684\u4e8c\u9032\u5236\u6578.<\/p>\n<p>\u88e1\u9762\u6700\u5de6\u908a\u7684 1bit \u662f\u7528 (0,1) \u4f5c\u6307\u5b9a \u6b63\u8ca0\u6578 (+\/-) \u6240\u4ee5\u9918\u4e0b\u7684\u9577\u5ea6\u5c31\u662f 31<\/p>\n<p>\u5373\u662f 2^31 \u6b21\u65b9 = 2147483648 \u5c31\u662f\u4f62\u7684\u9577\u5ea6, \u6240\u4ee5 0 ~ 2^31-1\u00a0\u5c31\u662f\u5b83\u7684\u53ef\u5132\u5b58\u7bc4\u570d 0~214748364<span style=\"color: #ff0000;\"><strong>7<\/strong><\/span>\u00a0. \u56e0\u70ba 0 \u672c\u8eab\u4f54\u4e00\u500b\u4f4d<\/p>\n<p>\u6b63\u6578\u7684\u7bc4\u570d\u5c31\u662f (0)2^31-1,<\/p>\n<p>\u8ca0\u6578\u7684\u7bc4\u570d\u5247\u662f (1)2^31 [0\u5728\u6b63\u6578\u7684\u7bc4\u570d\u6240\u4ee5\u8ca0\u6578\u6703\u591a\u4e00\u500b\u4f4d, Math bitch!]<\/p>\n<p>\u5982\u679c uint \u7684\u8a71\u56e0\u70ba\u591a\u4e86\u4e00\u500b bit \u505a 2\u9032\u5236\u904b\u7b97\u6240\u4ee5 2^32 \u7684\u9577\u5ea6\u5c31\u53ef\u4ee5\u76e1\u7528\u4e86<\/p>\n<p>\u5373\u662f 0~2^32-1 \u5373\u662f 4294967296 \u662f\u5b83\u7684\u4e0a\u9650.<br \/>\nPS: \u4efb\u53ef\u8d85\u904e 2^31 \u7684\u6578\u503c cast \u4f5c uint \u662f\u4e00\u5b9a\u5931\u6557\u7684 :D, \u56e0\u70ba\u6c92\u5730\u65b9\u5b58\u653e.<\/p>\n<p>\u7c21\u55ae\u8aaa int -&gt; uint \u5b89\u5168, \u4f46 uint -&gt; int \u8981\u7559\u610f\u6709\u6c92\u6709 overflow.<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4eca\u5929\u5728\u505a\u4e00\u500b 16 \u9032\u5236 Mask \u7684\u6642\u5019\u51fa\u4e86\u4e00\u500b Warning. private static  &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[16],"class_list":["post-1220","post","type-post","status-publish","format-standard","hentry","category-c","tag-c-2"],"_links":{"self":[{"href":"https:\/\/www.clonefactor.com\/wordpress\/wp-json\/wp\/v2\/posts\/1220","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.clonefactor.com\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.clonefactor.com\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.clonefactor.com\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.clonefactor.com\/wordpress\/wp-json\/wp\/v2\/comments?post=1220"}],"version-history":[{"count":0,"href":"https:\/\/www.clonefactor.com\/wordpress\/wp-json\/wp\/v2\/posts\/1220\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.clonefactor.com\/wordpress\/wp-json\/wp\/v2\/media?parent=1220"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.clonefactor.com\/wordpress\/wp-json\/wp\/v2\/categories?post=1220"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.clonefactor.com\/wordpress\/wp-json\/wp\/v2\/tags?post=1220"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}