Why does int8 left bitshift become negative on 8th bit?

https://play.golang.com/p/TyFQoBR9dQr

package main

import (
“fmt”
)

func main() {
var c int8 = 1 // 00000001
fmt.Println(c << 6) // 01000000 = 64
fmt.Println(c << 7) // 10000000 = -128 ???
}

Because the int8 is unsigned - so the last bit decides the direction

1 Like

It looks like you may have already figured it out, but please see Two’s complement - Wikipedia for more information on what’s happening.

2 Likes

if first bit starts with 1 it’s a negative number, otherwise a positive one

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.