Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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

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

Output:

11111100
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading