2016 Update

This commit is contained in:
Tina Müller 2016-12-05 22:15:40 +01:00
parent 948b86eafa
commit dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions

View file

@ -2,44 +2,37 @@ package main
import "fmt"
func bitwise(a, b int16) (and, or, xor, not, shl, shr, ras, rol, ror int16) {
// the first four are easy
and = a & b
or = a | b
xor = a ^ b
not = ^a
func bitwise(a, b int16) {
fmt.Printf("a: %016b\n", uint16(a))
fmt.Printf("b: %016b\n", uint16(b))
// for all shifts, the right operand (shift distance) must be unsigned.
// use abs(b) for a non-negative value.
if b < 0 {
b = -b
}
ub := uint(b)
// Bitwise logical operations
fmt.Printf("and: %016b\n", uint16(a&b))
fmt.Printf("or: %016b\n", uint16(a|b))
fmt.Printf("xor: %016b\n", uint16(a^b))
fmt.Printf("not: %016b\n", uint16(^a))
shl = a << ub
// for right shifts, if the left operand is unsigned, Go performs
// a logical shift; if signed, an arithmetic shift.
shr = int16(uint16(a) >> ub)
ras = a >> ub
if b < 0 {
fmt.Println("Right operand is negative, but all shifts require an unsigned right operand (shift distance).")
return
}
ua := uint16(a)
ub := uint32(b)
// rotates
rol = a << ub | int16(uint16(a) >> (16-ub))
ror = int16(uint16(a) >> ub) | a << (16-ub)
return
// Logical shifts (unsigned left operand)
fmt.Printf("shl: %016b\n", uint16(ua<<ub))
fmt.Printf("shr: %016b\n", uint16(ua>>ub))
// Arithmetic shifts (signed left operand)
fmt.Printf("las: %016b\n", uint16(a<<ub))
fmt.Printf("ras: %016b\n", uint16(a>>ub))
// Rotations
fmt.Printf("rol: %016b\n", uint16(a<<ub|int16(uint16(a)>>(16-ub))))
fmt.Printf("ror: %016b\n", uint16(int16(uint16(a)>>ub)|a<<(16-ub)))
}
func main() {
var a, b int16 = -460, 6
and, or, xor, not, shl, shr, ras, rol, ror := bitwise(a, b)
fmt.Printf("a: %016b\n", uint16(a))
fmt.Printf("b: %016b\n", uint16(b))
fmt.Printf("and: %016b\n", uint16(and))
fmt.Printf("or: %016b\n", uint16(or))
fmt.Printf("xor: %016b\n", uint16(xor))
fmt.Printf("not: %016b\n", uint16(not))
fmt.Printf("shl: %016b\n", uint16(shl))
fmt.Printf("shr: %016b\n", uint16(shr))
fmt.Printf("ras: %016b\n", uint16(ras))
fmt.Printf("rol: %016b\n", uint16(rol))
fmt.Printf("ror: %016b\n", uint16(ror))
var a, b int16 = -460, 6
bitwise(a, b)
}