         return String(repeating: char, count: diff) + self
     }
 }
+
+extension UInt8 {
+    func binaryDescription() -> String {
+        return String(self, radix: 2).pad(with: "0", length: 8)
+    }
+    
+    func pos8() -> UInt8 {
+        return (UInt8(1) << 3) & self
+    }
+    
+    func pos4() -> UInt8 {
+        return (UInt8(1) << 2) & self
+    }
+    
+    func pos2() -> UInt8 {
+        return (UInt8(1) << 1) & self
+    }
+    
+    func pos1() -> UInt8 {
+        return (UInt8(1) << 0) & self
+    }
+}

        super.viewDidLoad()

        // Do any additional setup after loading the view.
        display(value: 1)
    }

    override func viewDidAppear(_ animated: Bool) {

    func display(value: Int) {
        
        let v = UInt8(value)
        let mask4 = (UInt8(1) << 4) - 1 // 1 << 4 is just 2^4. Subtracting 1 gives us a mask of length 4.
        print(String(v & mask4, radix: 2).pad(with: "0", length: 8))
        
        let l1 = (v.pos8()) >> 3
        let l2 = (v.pos4()) >> 2
        let l3 = (v.pos2()) >> 1
        let l4 = (v.pos1()) >> 0
        
        let l1Not = (~l1)
        let l2Not = (~l2)
        let l3Not = (~l3)
        let l4Not = (~l4)
        
        // print((~l1).pos8(), ~l2 & (UInt8(1) << 2) & mask4, ~l3)
        // print(((~l1 & ~l2 & l3) & mask4).binaryDescription())
        print(l2,l3,l4Not)
        print((l2 & l3 & l4Not).binaryDescription())
        let gState = (l1Not & l2Not & l3) | (l1Not & l2 & l3Not) | (l2 & l3Not & l4) | (l2 & l3 & l4Not) | (l1 & l2Not) | (l1 & l3 & l4)
        
        print(gState.binaryDescription())
        
        // Turn off all segments to prepare for new value.
        _ = segments?.map({
