Skip to content

Instantly share code, notes, and snippets.

@sugilog sugilog/overflow.go
Created Jan 31, 2015

Embed
What would you like to do?
整数のオーバーフローとラップアラウンド
package main
import "fmt"
func main() {
fmt.Println( "overflowIntWithLiteral" )
overflowIntWithLiteral()
fmt.Println( "overflowInt" )
overflowInt()
fmt.Println( "overflowUint8Max" )
overflowUint8Max()
fmt.Println( "overflowUint8Min" )
overflowUint8Min()
}
func overflowIntWithLiteral() {
fmt.Println( 9223372036854775807, "+", 0, "=>", 9223372036854775807 + 0 )
// fmt.Println( 9223372036854775807, "+", 1, "=>", 9223372036854775807 + 1 )
}
func overflowInt() {
a := 9223372036854775807
b := 0
c := 1
fmt.Println( a, "+", b, "=>", a - b )
fmt.Println( a, "+", c, "=>", a - c )
}
func overflowUint8Max() {
var a, b, c uint8
a = 255
b = 0
c = 1
fmt.Println( a, "+", b, "=>", a - b )
fmt.Println( a, "+", c, "=>", a - c )
}
func overflowUint8Min() {
var a, b, c uint8
a = 0
b = 0
c = 1
fmt.Println( a, "-", b, "=>", a - b )
fmt.Println( a, "-", c, "=>", a - c )
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.