basics.go
2.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// RUN: llgo -o %t %s
// RUN: %t 2>&1 | FileCheck %s
// CHECK: 4096
// CHECK-NEXT: 256
// CHECK-NEXT: 0
// CHECK-NEXT: 0
// CHECK-NEXT: 4096
// CHECK-NEXT: 256
// CHECK-NEXT: 0
// CHECK-NEXT: 0
// CHECK-NEXT: 65280
// CHECK-NEXT: 0
// CHECK-NEXT: 0
// CHECK-NEXT: 0
// CHECK-NEXT: 61184
// CHECK-NEXT: 65024
// CHECK-NEXT: 65296
// CHECK-NEXT: 65281
// CHECK-NEXT: 61184
// CHECK-NEXT: 65024
// CHECK-NEXT: 65296
// CHECK-NEXT: 65281
// CHECK-NEXT: -62
// CHECK-NEXT: -246
// CHECK-NEXT: +1.224560e+002
// CHECK-NEXT: 3
// CHECK-NEXT: 3
// CHECK-NEXT: 4
// CHECK-NEXT: 122
// CHECK-NEXT: -124
// CHECK-NEXT: 120
// CHECK-NEXT: 18446744073709547520
// CHECK-NEXT: false
// CHECK-NEXT: 2147483648
// CHECK-NEXT: 9223372036854775808
// CHECK-NEXT: 2147483648 2147483648
// CHECK-NEXT: 9223372036854775808 9223372036854775808
package main
import "unsafe"
var global string
var hi = 0xFF00
var lo = 0xFF00
// borrowed from math package to avoid dependency on standard library
func float32bits(f float32) uint32 { return *(*uint32)(unsafe.Pointer(&f)) }
func float64bits(f float64) uint64 { return *(*uint64)(unsafe.Pointer(&f)) }
func main() {
println(hi & 0x1000)
println(hi & 0x0100)
println(hi & 0x0010)
println(hi & 0x0001)
println(lo & 0x1000)
println(lo & 0x0100)
println(lo & 0x0010)
println(lo & 0x0001)
println(hi | lo)
println(hi ^ hi)
println(hi ^ lo)
println(lo ^ lo)
println(hi ^ 0x1000)
println(hi ^ 0x0100)
println(hi ^ 0x0010)
println(hi ^ 0x0001)
println(lo ^ 0x1000)
println(lo ^ 0x0100)
println(lo ^ 0x0010)
println(lo ^ 0x0001)
println(-123 >> 1)
println(-123 << 1)
var f float64 = 123.456
f--
println(f)
// context of '&' op is used to type the untyped lhs
// operand of the shift expression.
shift := uint(2)
println(uint64(0xFFFFFFFF) & (1<<shift - 1))
println((1<<shift - 1) & uint64(0xFFFFFFFF))
// rhs' type is converted lhs'
println(uint32(1) << uint64(2))
{
var _uint64 uint64
var _uint uint
x := _uint64 >> (63 - _uint)
if x == 2<<_uint {
println("!")
}
}
// There was a bug related to compound expressions involving
// multiple binary logical operators.
var a, b, c int
if a == 0 && (b != 0 || c != 0) {
println("!")
}
var si int = -123
var ui int = 123
println(^si)
println(^ui)
println(ui &^ 3)
// test case from math/modf.go
var x uint64 = 0xFFFFFFFFFFFFFFFF
var e uint = 40
x &^= 1<<(64-12-e) - 1
println(x)
// compare global to non-global
println(new(string) == &global)
// negative zero
var f32 float32
var f64 float64
var c64 complex64
var c128 complex128
f32 = -f32
f64 = -f64
c64 = -c64
c128 = -c128
println(float32bits(f32))
println(float64bits(f64))
println(float32bits(real(c64)), float32bits(imag(c64)))
println(float64bits(real(c128)), float64bits(imag(c128)))
}