Skip to content

Instantly share code, notes, and snippets.

@crmackay
Last active August 29, 2015 14:16
Show Gist options
  • Save crmackay/f972c9496cdf990ef9d1 to your computer and use it in GitHub Desktop.
Save crmackay/f972c9496cdf990ef9d1 to your computer and use it in GitHub Desktop.
//I'm learning about bitwise flags and bitwise math (|, ^=, |=, etc) in golang...
//http://play.golang.org/p/QTJZJaOjZc
package main
import "fmt"
const (
one = 1 << iota // 1 = 0000 0001
two // 2 = 0000 0010
three // 4 = 0000 0100
)
func main() {
fmt.Printf("%b\n", one) // 1
fmt.Printf("%b\n", two) // 10
fmt.Printf("%b\n", three) // 100
fmt.Printf("%b\n", one|two|three) // 111
test := one | two | three
fmt.Printf("%b\n", test&two)
fmt.Printf("%c\n", test&two)
fmt.Printf("%d\n", test&two)
fmt.Printf("%o\n", test&two)
fmt.Printf("%q\n", test&two)
fmt.Printf("%x\n", test&two)
fmt.Printf("%X\n", test&two)
fmt.Printf("%U\n", test&two)
/* %b base 2
%c the character represented by the corresponding Unicode code point
%d base 10
%o base 8
%q a single-quoted character literal safely escaped with Go syntax.
%x base 16, with lower-case letters for a-f
%X base 16, with upper-case letters for A-F
%U Unicode format: U+1234; same as "U+%04X"
*/
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment