Converting from an integer to its binary representation

The strconv package has FormatInt, which accepts an int64 and lets you specify the base. n := int64(123) fmt.Println(strconv.FormatInt(n, 2)) // 1111011 DEMO: http://play.golang.org/p/leGVAELMhv http://golang.org/pkg/strconv/#FormatInt func FormatInt(i int64, base int) string FormatInt returns the string representation of i in the given base, for 2 <= base <= 36. The result uses the lower-case letters ‘a’ … Read more

Why prefer two’s complement over sign-and-magnitude for signed numbers?

It’s done so that addition doesn’t need to have any special logic for dealing with negative numbers. Check out the article on Wikipedia. Say you have two numbers, 2 and -1. In your “intuitive” way of representing numbers, they would be 0010 and 1001, respectively (I’m sticking to 4 bits for size). In the two’s … Read more

What is “2’s Complement”?

Two’s complement is a clever way of storing integers so that common math problems are very simple to implement. To understand, you have to think of the numbers in binary. It basically says, for zero, use all 0’s. for positive integers, start counting up, with a maximum of 2(number of bits – 1)-1. for negative … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)