update: bits example

This commit is contained in:
Inanc Gumus
2018-11-22 10:56:32 +03:00
parent d5fb944bc6
commit bc2b8b7c51

View File

@ -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)
}