diff --git a/09-go-type-system/01-bits/main.go b/09-go-type-system/01-bits/main.go index 1ce938d..33de6e2 100644 --- a/09-go-type-system/01-bits/main.go +++ b/09-go-type-system/01-bits/main.go @@ -7,7 +7,10 @@ package main -import "fmt" +import ( + "fmt" + "strconv" +) func main() { // %b verb prints bits @@ -43,4 +46,46 @@ func main() { fmt.Printf("%08b = %d\n", 32, 32) fmt.Printf("%08b = %d\n", 64, 64) fmt.Printf("%08b = %d\n", 128, 128) + + // ------------------------------------------------ + // How to calculate bits? + // ------------------------------------------------ + + // note: ^ means power of. + // for example: 2^2 means 2 * 2 = 4 + // or: 2^3 means 2 * 2 * 2 = 8 + + // 0 0 0 0 0 0 1 0 is equal to 2 because: + // ^ ^ ^ ^ ^ ^ ^ ^ + // | | | | | | | | + // | | | | | | | +-> 2^0 * 0 = 0 + // | | | | | | +---> 2^1 * 1 = 2 + // | | | | | +-----> 2^2 * 0 = 0 + // | | | | +-------> 2^3 * 0 = 0 + // | | | +---------> 2^4 * 0 = 0 + // | | +-----------> 2^5 * 0 = 0 + // | +-------------> 2^6 * 0 = 0 + // +---------------> 2^7 * 0 = 0 + // ------------+ + // 2 + + i, _ := strconv.ParseInt("00000010", 2, 64) + fmt.Println(i) + + // 0 0 0 1 0 1 1 0 is equal to 2 because: + // ^ ^ ^ ^ ^ ^ ^ ^ + // | | | | | | | | + // | | | | | | | +-> 2^0 * 0 = 0 + // | | | | | | +---> 2^1 * 1 = 2 + // | | | | | +-----> 2^2 * 1 = 4 + // | | | | +-------> 2^3 * 0 = 0 + // | | | +---------> 2^4 * 1 = 16 + // | | +-----------> 2^5 * 0 = 0 + // | +-------------> 2^6 * 0 = 0 + // +---------------> 2^7 * 0 = 0 + // ------------+ + // 22 + + i, _ = strconv.ParseInt("00010110", 2, 64) + fmt.Println(i) }