Representation of negative numbers in memory

In the below code:

package main

import (

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:


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)



Leave a Reply