if x.neg {
// (-x) & (-y) == ^(x-1) & ^(y-1) == ^((x-1) | (y-1)) == -(((x-1) | (y-1)) + 1)
x1 := nat{}.sub(x.abs, natOne)
- y1 := z.abs.sub(y.abs, natOne)
+ y1 := nat{}.sub(y.abs, natOne)
z.abs = z.abs.add(z.abs.or(x1, y1), natOne)
z.neg = true // z cannot be zero if x and y are negative
return z
}
// x & (-y) == x & ^(y-1) == x &^ (y-1)
- y1 := z.abs.sub(y.abs, natOne)
+ y1 := nat{}.sub(y.abs, natOne)
z.abs = z.abs.andNot(x.abs, y1)
z.neg = false
return z
if x.neg {
// (-x) &^ (-y) == ^(x-1) &^ ^(y-1) == ^(x-1) & (y-1) == (y-1) &^ (x-1)
x1 := nat{}.sub(x.abs, natOne)
- y1 := z.abs.sub(y.abs, natOne)
+ y1 := nat{}.sub(y.abs, natOne)
z.abs = z.abs.andNot(y1, x1)
z.neg = false
return z
if x.neg {
// (-x) &^ y == ^(x-1) &^ y == ^(x-1) & ^y == ^((x-1) | y) == -(((x-1) | y) + 1)
- x1 := z.abs.sub(x.abs, natOne)
+ x1 := nat{}.sub(x.abs, natOne)
z.abs = z.abs.add(z.abs.or(x1, y.abs), natOne)
z.neg = true // z cannot be zero if x is negative and y is positive
return z
}
// x &^ (-y) == x &^ ^(y-1) == x & (y-1)
- y1 := z.abs.add(y.abs, natOne)
+ y1 := nat{}.add(y.abs, natOne)
z.abs = z.abs.and(x.abs, y1)
z.neg = false
return z
if x.neg {
// (-x) | (-y) == ^(x-1) | ^(y-1) == ^((x-1) & (y-1)) == -(((x-1) & (y-1)) + 1)
x1 := nat{}.sub(x.abs, natOne)
- y1 := z.abs.sub(y.abs, natOne)
+ y1 := nat{}.sub(y.abs, natOne)
z.abs = z.abs.add(z.abs.and(x1, y1), natOne)
z.neg = true // z cannot be zero if x and y are negative
return z
}
// x | (-y) == x | ^(y-1) == ^((y-1) &^ x) == -(^((y-1) &^ x) + 1)
- y1 := z.abs.sub(y.abs, natOne)
+ y1 := nat{}.sub(y.abs, natOne)
z.abs = z.abs.add(z.abs.andNot(y1, x.abs), natOne)
z.neg = true // z cannot be zero if one of x or y is negative
return z
if x.neg {
// (-x) ^ (-y) == ^(x-1) ^ ^(y-1) == (x-1) ^ (y-1)
x1 := nat{}.sub(x.abs, natOne)
- y1 := z.abs.sub(y.abs, natOne)
+ y1 := nat{}.sub(y.abs, natOne)
z.abs = z.abs.xor(x1, y1)
z.neg = false
return z
}
// x ^ (-y) == x ^ ^(y-1) == ^(x ^ (y-1)) == -((x ^ (y-1)) + 1)
- y1 := z.abs.sub(y.abs, natOne)
+ y1 := nat{}.sub(y.abs, natOne)
z.abs = z.abs.add(z.abs.xor(x.abs, y1), natOne)
z.neg = true // z cannot be zero if only one of x or y is negative
return z
bitwiseTest{"0x00", "0x01", "0x00", "0x01", "0x01", "0x00"},
bitwiseTest{"0x01", "0x00", "0x00", "0x01", "0x01", "0x01"},
bitwiseTest{"-0x01", "0x00", "0x00", "-0x01", "-0x01", "-0x01"},
- bitwiseTest{"-0xAF", "-0x50", "0x00", "-0xFF", "-0x01", "-0x01"},
+ bitwiseTest{"-0xaf", "-0x50", "-0xf0", "-0x0f", "0xe1", "0x41"},
bitwiseTest{"0x00", "-0x01", "0x00", "-0x01", "-0x01", "0x00"},
bitwiseTest{"0x01", "0x01", "0x01", "0x01", "0x00", "0x00"},
bitwiseTest{"-0x01", "-0x01", "-0x01", "-0x01", "0x00", "0x00"},
func testBitFun(t *testing.T, msg string, f bitFun, x, y *Int, exp string) {
expected := new(Int)
- expected.SetString(exp, 16)
+ expected.SetString(exp, 0)
out := f(new(Int), x, y)
if out.Cmp(expected) != 0 {
- println("Test failed")
t.Errorf("%s: got %s want %s", msg, out, expected)
}
}
func testBitFunSelf(t *testing.T, msg string, f bitFun, x, y *Int, exp string) {
+ self := new(Int)
+ self.Set(x)
expected := new(Int)
- expected.SetString(exp, 16)
+ expected.SetString(exp, 0)
- x = f(x, x, y)
- if x.Cmp(expected) != 0 {
- println("Test failed")
- t.Errorf("%s: got %s want %s", msg, x, expected)
+ self = f(self, self, y)
+ if self.Cmp(expected) != 0 {
+ t.Errorf("%s: got %s want %s", msg, self, expected)
}
}
x := new(Int)
y := new(Int)
for _, test := range bitwiseTests {
- x.SetString(test.x, 16)
- y.SetString(test.y, 16)
+ x.SetString(test.x, 0)
+ y.SetString(test.y, 0)
testBitFun(t, "and", (*Int).And, x, y, test.and)
testBitFunSelf(t, "and", (*Int).And, x, y, test.and)
n, m = m, n
s = y
}
- // n >= m
+ // m >= n
- z = z.make(n)
- for i := 0; i < m; i++ {
+ z = z.make(m)
+ for i := 0; i < n; i++ {
z[i] = x[i] | y[i]
}
- copy(z[m:n], s[m:n])
+ copy(z[n:m], s[n:m])
return z.norm()
}
m := len(x)
n := len(y)
s := x
- if n < m {
+ if m < n {
n, m = m, n
s = y
}
- // n >= m
+ // m >= n
- z = z.make(n)
- for i := 0; i < m; i++ {
+ z = z.make(m)
+ for i := 0; i < n; i++ {
z[i] = x[i] ^ y[i]
}
- copy(z[m:n], s[m:n])
+ copy(z[n:m], s[n:m])
return z.norm()
}