Skip to content

Instantly share code, notes, and snippets.

@sugilog
Created January 31, 2015 08:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sugilog/6adda2e31a7093f6b42e to your computer and use it in GitHub Desktop.
Save sugilog/6adda2e31a7093f6b42e to your computer and use it in GitHub Desktop.
整数のオーバーフローとラップアラウンド
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