{"id":1958,"date":"2018-08-06T17:49:15","date_gmt":"2018-08-06T09:49:15","guid":{"rendered":"http:\/\/www.clonefactor.com\/wordpress\/?p=1958"},"modified":"2018-08-06T17:49:50","modified_gmt":"2018-08-06T09:49:50","slug":"ispoweroftwo","status":"publish","type":"post","link":"https:\/\/www.clonefactor.com\/wordpress\/program\/c\/1958\/","title":{"rendered":"\u5feb\u901f\u6aa2\u67e5 enum \u662f\u5426\u70ba\u57fa\u790e\u503c &#8211; IsPowerOfTwo"},"content":{"rendered":"<p>\u6aa2\u67e5 Enum bitmask \u7684\u6642\u5019\u7d93\u5e38\u9700\u8981\u6aa2\u67e5 Enum \u662f\u5426\u55ae\u4e00\u6216\u8005\u5305\u542b\u591a\u500b\u503c.<br \/>\n<span style=\"font-size: 1.4rem;\">\u9019\u6642\u5019 IsPowerOfTwo \u5c31\u662f\u9700\u8981\u7684\u7b97\u5f0f\u4e86, \u627e\u5230\u4e00\u500b\u5f88\u9ad8\u6548\u7684\u904b\u7b97\u6cd5, \u7b46\u8a18\u4e00\u4e0b.<\/span><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">public bool IsPowerOfTwo(long value)\r\n{\r\n\treturn (value != 0) &amp;&amp; ((value &amp; (value - 1)) == 0);\r\n}<\/pre>\n<p>\u516c\u5f0f\u5f88\u7c21\u55ae\u4f46\u5167\u88e1\u7684\u60f3\u6cd5\u5f88\u6df1\u5165.<\/p>\n<p>\u7b2c\u4e00\u7bc0\u00a0value != 0 \u5f88\u7c21\u55ae\u7684\u5c31\u662f\u9664\u53bb 0, \u56e0\u70ba bitmask \u5c31\u662f\u4e0d\u5e360\u7684<\/p>\n<p>\u7b2c\u4e8c\u7bc0 ((value &amp; (value-1)) == 0 \u76f4\u63a5\u7528\u4e0a\u4e8c\u9032\u5236\u7684\u6e1b\u6cd5\u6a21\u5f0f\u8ddf\u5341\u9032\u5236\u7684\u904b\u7b97.<\/p>\n<p>\u9019\u908a\u8aaa\u4e00\u4e0b\u4e8c\u9032\u5236\u7684\u6e1b\u6cd5\u6a21\u5f0f :<\/p>\n<p><span style=\"font-size: 1.4rem;\">\u9019\u908a\u6211\u5011\u77e5\u9053 1,2,4,8 \u662f2\u7684\u6b21\u65b9\u6578,<br \/>\n<\/span>\u4e8c\u9032\u5236\u7684\u6b21\u65b9\u6578\u6e1b\u53bb\u300c\u4e00\u300d\u7684\u6642\u5019\u5c31\u525b\u597d\u662f\u628a\u6574\u7d44\u6578\u5b57\u53f3\u65b9\u7684\u96f6\u90fd\u586b\u4e0a 1 \u7684\u60c5\u6cc1.<br \/>\n\u7a0d\u70ba\u5217\u4e00\u4e0b\u6b21\u65b9\u6578\u7684\u6a21\u5f0f.<\/p>\n<p>e.g. 8 = 1000, 8-1 = 0111<br \/>\ne.g. 4 = 0100, 4-1 = 0011<br \/>\ne.g. 2 = 0010, 2-1 = 0001<br \/>\ne.g. 1 = 0001, 1-1 = 0000<\/p>\n<p>\u5f88\u660e\u986f\u7684\u6a21\u5f0f, \u800c\u9019\u500b\u7a0b\u5f0f\u7684\u6aa2\u67e5\u6cd5\u5c31\u662f\u628a\u6e1b\u300c\u4e00\u300d\u524d\u5f8c\u7684\u5169\u7d44\u4e8c\u9032\u5236\u6578\u5b57\u505a And \u6bd4\u5c0d.<br \/>\n\u7d50\u679c\u7576\u7136\u662f\u6c38\u9060\u7b49\u65bc\u300c\u96f6\u300d<\/p>\n<h2>\u61c9\u7528<\/h2>\n<p>\u904a\u6232\u6642\u5229\u7528\u9019\u500b IsPowerOfTwo, \u6211\u5011\u53ef\u4ee5\u5f88\u7c21\u55ae\u7684\u4ecb\u5b9a\u4e00\u4e0b\u50b7\u5bb3\u5c64<\/p>\n<p>\u5047\u8a2d\u904a\u6232\u4e2d\u7684\u5c0d\u50b7\u5bb3\u7684\u5224\u5b9a\u503c\u662f\u9019\u500b :<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">[System.Flags]\r\npublic enum eDamageLayerMask\r\n{\r\n\tNone = 0,\r\n\tPlayer = 1 &lt;&lt; 0,\r\n\tNeutral = 1 &lt;&lt; 2,\r\n\r\n\tGroupA = Player | Alliance,\r\n\tGroupB = Enemies | Alliance,\r\n\tGroupC = Enemies | Neutral,\r\n\r\n\tAlliance = 1 &lt;&lt; 1,\r\n\tEnemies = 1 &lt;&lt; 3,\r\n\r\n\tALL = Player | Alliance | Neutral | Enemies\r\n}<\/pre>\n<p>\u5728\u5e73\u8861\u8a2d\u5b9a\u6642\u6211\u5011\u6703\u5e0c\u671b\u53ea\u80fd\u5920\u5728 { Player, Neutral, Alliance, Enemies } \u56db\u500b\u503c\u4e4b\u9593\u9078,<\/p>\n<p>\u4f46\u540c\u6642\u67d0\u4e9b\u6642\u5019\u53c8\u5e0c\u671b\u53ef\u4ee5\u7c21\u55ae\u7684\u6aa2\u67e5\u662f\u5426\u70ba <code>eDamageLayerMask.GroupC<\/code> \u4e4b\u985e\u7684\u60c5\u6cc1.<\/p>\n<p>\u9019\u6a23\u5c31\u53ef\u4ee5\u7c21\u55ae\u7684\u5206\u8fa6\u51fa\u57fa\u790e\u5143\u7d20\u53ca\u591a\u91cd\u5143\u7d20\u7684\u5206\u5225.<\/p>\n<h2>\u5f8c\u8a18<\/h2>\n<p>\u4e00\u500b\u7c21\u55ae\u7684 wrapper \u628a Unity3D \u7684 EditorGUI.MaskField \u505a\u6210 custom property drawer.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">public class MaskFieldAttribute : UnityEngine.PropertyAttribute\r\n{\r\n\tpublic readonly System.Type type;\r\n\tpublic MaskFieldAttribute(System.Type enumType)\r\n\t{\r\n\t\tthis.type = enumType;\r\n\t}\r\n}<\/pre>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">[CustomPropertyDrawer(typeof(MaskFieldAttribute))]\r\npublic class MaskFieldDrawer : PropertyDrawer\r\n{\r\n\tMaskFieldAttribute maskFieldAttribute { get { return (MaskFieldAttribute)attribute; } }\r\n\r\n\tstring[] m_Options = null;\r\n\r\n\tpublic override void OnGUI(Rect position, SerializedProperty property, GUIContent label)\r\n\t{\r\n\t\tEditorGUI.BeginProperty(position, label, property);\r\n\t\tif (property.propertyType == SerializedPropertyType.Enum)\r\n\t\t{\r\n\t\t\tif (m_Options == null)\r\n\t\t\t{\r\n\t\t\t\tvar labels = System.Enum.GetNames(maskFieldAttribute.type);\r\n\t\t\t\tvar values = System.Enum.GetValues(maskFieldAttribute.type).GetEnumerator();\r\n\t\t\t\tList&lt;string&gt; tmp = new List&lt;string&gt;();\r\n\t\t\t\tint index = 0;\r\n\t\t\t\twhile (values.MoveNext())\r\n\t\t\t\t{\r\n\t\t\t\t\t\/\/ filter out multiple enum tag\r\n\t\t\t\t\tint enumValue = (int)values.Current;\r\n\t\t\t\t\tif (IsPowerOfTwo(enumValue))\r\n\t\t\t\t\t\ttmp.Add(labels[index]);\r\n\t\t\t\t\tindex++;\r\n\t\t\t\t}\r\n\t\t\t\tm_Options = tmp.ToArray();\r\n\t\t\t}\r\n\t\t\tEditorGUI.BeginChangeCheck();\r\n\t\t\tint rst = EditorGUI.MaskField(position, property.intValue, m_Options);\r\n\t\t\tif (EditorGUI.EndChangeCheck())\r\n\t\t\t{\r\n\t\t\t\tproperty.intValue = rst;\r\n\t\t\t}\r\n\t\t}\r\n\t\telse\r\n\t\t{\r\n\t\t\tEditorGUI.LabelField(position, label, typeof(MaskFieldAttribute).Name + \" only allow to use with { Enum }.\");\r\n\t\t}\r\n\t\tEditorGUI.EndProperty();\r\n\t}\r\n\r\n\tpublic bool IsPowerOfTwo(long value)\r\n\t{\r\n\t\treturn (value != 0) &amp;&amp; ((value &amp; (value - 1)) == 0);\r\n\t}\r\n}<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6aa2\u67e5 Enum bitmask \u7684\u6642\u5019\u7d93\u5e38\u9700\u8981\u6aa2\u67e5 Enum \u662f\u5426\u55ae\u4e00\u6216\u8005\u5305\u542b\u591a\u500b\u503c. \u9019\u6642\u5019 Is &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,78,11],"tags":[20,43],"class_list":["post-1958","post","type-post","status-publish","format-standard","hentry","category-c","category-math","category-unity3d","tag-editor","tag-unity3d-2"],"_links":{"self":[{"href":"https:\/\/www.clonefactor.com\/wordpress\/wp-json\/wp\/v2\/posts\/1958","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=1958"}],"version-history":[{"count":0,"href":"https:\/\/www.clonefactor.com\/wordpress\/wp-json\/wp\/v2\/posts\/1958\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.clonefactor.com\/wordpress\/wp-json\/wp\/v2\/media?parent=1958"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.clonefactor.com\/wordpress\/wp-json\/wp\/v2\/categories?post=1958"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.clonefactor.com\/wordpress\/wp-json\/wp\/v2\/tags?post=1958"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}