{"id":130,"date":"2016-03-19T12:04:39","date_gmt":"2016-03-19T11:04:39","guid":{"rendered":"https:\/\/solidt.eu\/blog\/?p=130"},"modified":"2019-10-28T15:21:01","modified_gmt":"2019-10-28T14:21:01","slug":"c-modifiy-a-byte-by-bit","status":"publish","type":"post","link":"https:\/\/solidt.eu\/site\/c-modifiy-a-byte-by-bit\/","title":{"rendered":"C# Modify a byte by bit"},"content":{"rendered":"\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\ufeffusing System;\nusing System.Collections.Generic;\nusing System.Text;\n\npublic class BitState\n{\n    public BitState()\n    {\n        \n    }\n\n    public BitState(byte aValue)\n    {\n        Value = aValue;\n    }\n\n    public byte Value = 0;\n\n    public void AddBit(int bitNumber)\n    {\n        \/\/ Binary or\n        Value = (byte)(Value | GetBit(bitNumber));\n    }\n\n    public bool HasBit(int bitNumber)\n    {\n        \/\/ Binary and\n        return (Value &amp; GetBit(bitNumber)) == GetBit(bitNumber);\n    }\n\n    public byte GetBit(int bitNumber)\n    {\n        \/\/ Shift left\n        return (byte)(1 &lt;&lt; bitNumber);\n    }\n\n    public void ToggleBit(int bitNumber)\n    {\n        \/\/ Binary xor\n        Value = (byte)(Value ^ GetBit(bitNumber));\n    }\n\n    public void RemoveBit(int bitNumber)\n    {\n        \/\/ Get bit to remove => invert => apply AND\t\t\t\n        Value = (byte)(Value &amp; ~GetBit(bitNumber));\n    }\n\n    public void InvertBits()\n    {\n        Value = unchecked((byte)~Value);\n    }\n\n    public void Clear()\n    {\n        Value = 0;\n    }\n\n    public void SetAll()\n    {\n        Value = 255;\n        \/\/Value = 0;\n        \/\/InvertBits();\n    }\n\n    public override string ToString()\n    {\n        return Convert.ToString(this.Value, 2).PadLeft(8, '0');\n    }\n\n    public bool[] AsBoolArray()\n    {\n        int sizeInBits = 8;\n        bool[] boolArray = new bool[sizeInBits];\n        for (int i = 0; i &lt; sizeInBits; i++)\n            boolArray[i] = HasBit(i);\n        return boolArray;\n    }\n}\n<\/pre>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[6],"tags":[],"class_list":["post-130","post","type-post","status-publish","format-standard","hentry","category-dotnet"],"_links":{"self":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/130","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/comments?post=130"}],"version-history":[{"count":4,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/130\/revisions"}],"predecessor-version":[{"id":2743,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/130\/revisions\/2743"}],"wp:attachment":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/media?parent=130"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/categories?post=130"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/tags?post=130"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}