Representation of negative numbers in memory

In the below code:

package main

import (
    "fmt"
)

func main() {

    var a, b int8 = -4, 4
    fmt.Printf("%b\n", a)

    fmt.Printf("%08b\n", a)

    fmt.Printf("%08b\n", b)
    //fmt.Println("a and b have same sign?", (a^b) >= 0)
}

gives output with minus sign:

-100
-0000100
00000100

How to view binary representation of negative number -4?

>Solution :

To see two’s compliment binary representation, this would work:

var aUnsigned = uint8(a)
fmt.Printf("%08b\n", aUnsigned)

Output:

11111100

Leave a Reply