Language 10 of 10 · Topic 0.4

Operators & Bit Clear Operator (&^ in Go)

1Concept

Go supports standard arithmetic and bitwise operators, including the unique Bit Clear operator &^ (AND NOT) which clears specific bits based on a mask.

2Architecture Diagram

Bit Clear (a &^ b):
 a = 0110 (6)
 b = 0010 (2)
 a &^ b = 0100 (4) [Bit 1 cleared!]

3Code Example

Stage 0 Language Foundations
package main

import "fmt"

func main() {
    var flags byte = 0b00001111 // 15
    var clearMask byte = 0b00000010 // Clear bit 1

    result := flags &^ clearMask
    fmt.Printf("Original: %08b (%d)\n", flags, flags)
    fmt.Printf("After Bit Clear (&^): %08b (%d)\n", result, result)
}

4Expected Output

Original: 00001111 (15)
After Bit Clear (&^): 00001101 (13)

5Key Takeaways

  • &^ (Bit Clear) provides clean hardware flag resetting.
  • Logical && and || short-circuit.
  • Go does not support ternary conditional operators (condition ? a : b).