Created
January 31, 2015 08:29
-
-
Save sugilog/6adda2e31a7093f6b42e to your computer and use it in GitHub Desktop.
整数のオーバーフローとラップアラウンド
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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