Tsallaka zuwa abun ciki
Justice Tech School

Bitwise Operators

Operators & | ^ &^ << >> work on bits, and &^ clears bits. iota with shifts builds flag sets.

Example
package main

import "fmt"

type Perm uint8

const (
	Read Perm = 1 << iota
	Write
	Exec
)

func main() {
	p := Read | Exec
	fmt.Println(p, p&Write != 0, p&Exec != 0)
	p |= Write
	p &^= Read
	fmt.Println(p)
}

Result

5 false true
6

Exercise

Test a flag.

p  Write != 0

Quiz Test

What does iota with << build?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.