本文整理汇总了Golang中math/big.Int.Not方法的典型用法代码示例。如果您正苦于以下问题:Golang Int.Not方法的具体用法?Golang Int.Not怎么用?Golang Int.Not使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类math/big.Int
的用法示例。
在下文中一共展示了Int.Not方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: UnaryOp
// UnaryOp returns the result of the unary expression op y.
// The operation must be defined for the operand.
// If size >= 0 it specifies the ^ (xor) result size in bytes.
// If y is Unknown, the result is Unknown.
//
func UnaryOp(op token.Token, y Value, size int) Value {
switch op {
case token.ADD:
switch y.(type) {
case unknownVal, int64Val, intVal, floatVal, complexVal:
return y
}
case token.SUB:
switch y := y.(type) {
case unknownVal:
return y
case int64Val:
if z := -y; z != y {
return z // no overflow
}
return normInt(new(big.Int).Neg(big.NewInt(int64(y))))
case intVal:
return normInt(new(big.Int).Neg(y.val))
case floatVal:
return normFloat(new(big.Rat).Neg(y.val))
case complexVal:
return normComplex(new(big.Rat).Neg(y.re), new(big.Rat).Neg(y.im))
}
case token.XOR:
var z big.Int
switch y := y.(type) {
case unknownVal:
return y
case int64Val:
z.Not(big.NewInt(int64(y)))
case intVal:
z.Not(y.val)
default:
goto Error
}
// For unsigned types, the result will be negative and
// thus "too large": We must limit the result size to
// the type's size.
if size >= 0 {
s := uint(size) * 8
z.AndNot(&z, new(big.Int).Lsh(big.NewInt(-1), s)) // z &^= (-1)<<s
}
return normInt(&z)
case token.NOT:
switch y := y.(type) {
case unknownVal:
return y
case boolVal:
return !y
}
}
Error:
panic(fmt.Sprintf("invalid unary operation %s%v", op, y))
}
示例2: unaryOpConst
// unaryOpConst returns the result of the constant evaluation op x where x is of the given type.
func unaryOpConst(x interface{}, op token.Token, typ *Basic) interface{} {
switch op {
case token.ADD:
return x // nothing to do
case token.SUB:
switch x := x.(type) {
case int64:
if z := -x; z != x {
return z // no overflow
}
// overflow - need to convert to big.Int
return normalizeIntConst(new(big.Int).Neg(big.NewInt(x)))
case *big.Int:
return normalizeIntConst(new(big.Int).Neg(x))
case *big.Rat:
return normalizeRatConst(new(big.Rat).Neg(x))
case complex:
return newComplex(new(big.Rat).Neg(x.re), new(big.Rat).Neg(x.im))
}
case token.XOR:
var z big.Int
switch x := x.(type) {
case int64:
z.Not(big.NewInt(x))
case *big.Int:
z.Not(x)
default:
unreachable()
}
// For unsigned types, the result will be negative and
// thus "too large": We must limit the result size to
// the type's size.
if typ.Info&IsUnsigned != 0 {
s := uint(typ.Size) * 8
if s == 0 {
// platform-specific type
// TODO(gri) this needs to be factored out
switch typ.Kind {
case Uint:
s = intBits
case Uintptr:
s = ptrBits
default:
unreachable()
}
}
// z &^= (-1)<<s
z.AndNot(&z, new(big.Int).Lsh(big.NewInt(-1), s))
}
return normalizeIntConst(&z)
case token.NOT:
return !x.(bool)
}
unreachable()
return nil
}
示例3: UnaryOp
// UnaryOp does a unary operation on a unary expression.
func UnaryOp(op scan.Type, y Value, prec uint) Value {
switch op {
case scan.Plus:
switch y.(type) {
case unknownVal, int64Val, intVal:
return y
}
case scan.Minus:
switch y := y.(type) {
case unknownVal:
return y
case int64Val:
if z := -y; z != y {
return z // no overflow
}
return normInt(new(big.Int).Neg(big.NewInt(int64(y))))
case intVal:
return normInt(new(big.Int).Neg(y.val))
}
case scan.Negate:
var z big.Int
switch y := y.(type) {
case unknownVal:
return y
case int64Val:
z.Not(big.NewInt(int64(y)))
case intVal:
z.Not(y.val)
default:
goto Error
}
return normInt(&z)
case scan.Not:
switch y := y.(type) {
case unknownVal:
return y
case int64Val:
if y == 0 {
return int64Val(1)
}
return int64Val(0)
case intVal:
z := new(big.Int).SetInt64(0)
if y.val.Sign() == 0 {
z.SetInt64(1)
}
return normInt(z)
}
}
Error:
panic(fmt.Sprintf("invalid unary operation %s%v", op, y))
}
示例4: unaryIntOp
func unaryIntOp(x *big.Int, op token.Token) interface{} {
var z big.Int
switch op {
case token.ADD:
return z.Set(x)
case token.SUB:
return z.Neg(x)
case token.XOR:
return z.Not(x)
}
panic("unreachable")
}
示例5: unaryOpConst
// unaryOpConst returns the result of the constant evaluation op x where x is of the given type.
func unaryOpConst(x interface{}, ctxt *Context, op token.Token, typ *Basic) interface{} {
switch op {
case token.ADD:
return x // nothing to do
case token.SUB:
switch x := x.(type) {
case int64:
if z := -x; z != x {
return z // no overflow
}
// overflow - need to convert to big.Int
return normalizeIntConst(new(big.Int).Neg(big.NewInt(x)))
case *big.Int:
return normalizeIntConst(new(big.Int).Neg(x))
case *big.Rat:
return normalizeRatConst(new(big.Rat).Neg(x))
case Complex:
return newComplex(new(big.Rat).Neg(x.Re), new(big.Rat).Neg(x.Im))
}
case token.XOR:
var z big.Int
switch x := x.(type) {
case int64:
z.Not(big.NewInt(x))
case *big.Int:
z.Not(x)
default:
unreachable()
}
// For unsigned types, the result will be negative and
// thus "too large": We must limit the result size to
// the type's size.
if typ.Info&IsUnsigned != 0 {
s := uint(ctxt.sizeof(typ)) * 8
z.AndNot(&z, new(big.Int).Lsh(big.NewInt(-1), s)) // z &^= (-1)<<s
}
return normalizeIntConst(&z)
case token.NOT:
return !x.(bool)
}
unreachable()
return nil
}
示例6: call
//.........这里部分代码省略.........
stack.Push(Zero256)
dbg.Printf(" %v %% %v = %v\n", x, y, 0)
} else {
xb := new(big.Int).SetBytes(x[:])
yb := new(big.Int).SetBytes(y[:])
zb := new(big.Int).SetBytes(z[:])
mul := new(big.Int).Mul(xb, yb)
mod := new(big.Int).Mod(mul, zb)
res := LeftPadWord256(U256(mod).Bytes())
stack.Push(res)
dbg.Printf(" %v * %v %% %v = %v (%X)\n",
xb, yb, zb, mod, res)
}
case EXP: // 0x0A
x, y := stack.Pop(), stack.Pop()
xb := new(big.Int).SetBytes(x[:])
yb := new(big.Int).SetBytes(y[:])
pow := new(big.Int).Exp(xb, yb, big.NewInt(0))
res := LeftPadWord256(U256(pow).Bytes())
stack.Push(res)
dbg.Printf(" %v ** %v = %v (%X)\n", xb, yb, pow, res)
case SIGNEXTEND: // 0x0B
back := stack.Pop()
backb := new(big.Int).SetBytes(back[:])
if backb.Cmp(big.NewInt(31)) < 0 {
bit := uint(backb.Uint64()*8 + 7)
num := stack.Pop()
numb := new(big.Int).SetBytes(num[:])
mask := new(big.Int).Lsh(big.NewInt(1), bit)
mask.Sub(mask, big.NewInt(1))
if numb.Bit(int(bit)) == 1 {
numb.Or(numb, mask.Not(mask))
} else {
numb.Add(numb, mask)
}
res := LeftPadWord256(U256(numb).Bytes())
dbg.Printf(" = %v (%X)", numb, res)
stack.Push(res)
}
case LT: // 0x10
x, y := stack.Pop(), stack.Pop()
xb := new(big.Int).SetBytes(x[:])
yb := new(big.Int).SetBytes(y[:])
if xb.Cmp(yb) < 0 {
stack.Push64(1)
dbg.Printf(" %v < %v = %v\n", xb, yb, 1)
} else {
stack.Push(Zero256)
dbg.Printf(" %v < %v = %v\n", xb, yb, 0)
}
case GT: // 0x11
x, y := stack.Pop(), stack.Pop()
xb := new(big.Int).SetBytes(x[:])
yb := new(big.Int).SetBytes(y[:])
if xb.Cmp(yb) > 0 {
stack.Push64(1)
dbg.Printf(" %v > %v = %v\n", xb, yb, 1)
} else {
stack.Push(Zero256)
dbg.Printf(" %v > %v = %v\n", xb, yb, 0)
}
示例7: not
func not(z *big.Int, x *big.Int, y *big.Int) *big.Int {
_ = y
return z.Not(x)
}
示例8: nxor
func nxor(z *big.Int, x *big.Int, y *big.Int) *big.Int {
z.Xor(x, y)
return z.Not(z)
}
示例9: Run
//.........这里部分代码省略.........
if y.Cmp(common.Big0) == 0 {
base.Set(common.Big0)
} else {
n := new(big.Int)
if x.Cmp(common.Big0) < 0 {
n.SetInt64(-1)
} else {
n.SetInt64(1)
}
base.Mod(x.Abs(x), y.Abs(y)).Mul(base, n)
U256(base)
}
stack.push(base)
case EXP:
x, y := stack.pop(), stack.pop()
base.Exp(x, y, Pow256)
U256(base)
stack.push(base)
case SIGNEXTEND:
back := stack.pop()
if back.Cmp(big.NewInt(31)) < 0 {
bit := uint(back.Uint64()*8 + 7)
num := stack.pop()
mask := new(big.Int).Lsh(common.Big1, bit)
mask.Sub(mask, common.Big1)
if common.BitTest(num, int(bit)) {
num.Or(num, mask.Not(mask))
} else {
num.And(num, mask)
}
num = U256(num)
stack.push(num)
}
case NOT:
stack.push(U256(new(big.Int).Not(stack.pop())))
case LT:
x, y := stack.pop(), stack.pop()
// x < y
if x.Cmp(y) < 0 {
stack.push(common.BigTrue)
} else {
stack.push(common.BigFalse)
}
case GT:
x, y := stack.pop(), stack.pop()
// x > y
if x.Cmp(y) > 0 {
stack.push(common.BigTrue)
} else {
stack.push(common.BigFalse)
}
case SLT:
x, y := S256(stack.pop()), S256(stack.pop())
示例10: Run
//.........这里部分代码省略.........
n.SetInt64(-1)
} else {
n.SetInt64(1)
}
base.Mod(x.Abs(x), y.Abs(y)).Mul(base, n)
U256(base)
}
self.Printf(" = %v", base)
stack.push(base)
case EXP:
x, y := stack.pop(), stack.pop()
self.Printf(" %v ** %v", x, y)
base.Exp(x, y, Pow256)
U256(base)
self.Printf(" = %v", base)
stack.push(base)
case SIGNEXTEND:
back := stack.pop()
if back.Cmp(big.NewInt(31)) < 0 {
bit := uint(back.Uint64()*8 + 7)
num := stack.pop()
mask := new(big.Int).Lsh(common.Big1, bit)
mask.Sub(mask, common.Big1)
if common.BitTest(num, int(bit)) {
num.Or(num, mask.Not(mask))
} else {
num.And(num, mask)
}
num = U256(num)
self.Printf(" = %v", num)
stack.push(num)
}
case NOT:
stack.push(U256(new(big.Int).Not(stack.pop())))
//base.Sub(Pow256, stack.pop()).Sub(base, common.Big1)
//base = U256(base)
//stack.push(base)
case LT:
x, y := stack.pop(), stack.pop()
self.Printf(" %v < %v", x, y)
// x < y
if x.Cmp(y) < 0 {
stack.push(common.BigTrue)
} else {
stack.push(common.BigFalse)
}
case GT:
x, y := stack.pop(), stack.pop()
self.Printf(" %v > %v", x, y)
// x > y
if x.Cmp(y) > 0 {
stack.push(common.BigTrue)
} else {
示例11: Eval
// Post-order traversal, equivalent to postfix notation.
func Eval(node interface{}) (*big.Int, error) {
switch nn := node.(type) {
case *ast.BinaryExpr:
z := new(big.Int)
x, xerr := Eval(nn.X)
if xerr != nil {
return nil, xerr
}
y, yerr := Eval(nn.Y)
if yerr != nil {
return nil, yerr
}
switch nn.Op {
case token.ADD:
return z.Add(x, y), nil
case token.SUB:
return z.Sub(x, y), nil
case token.MUL:
return z.Mul(x, y), nil
case token.QUO:
if y.Sign() == 0 { // 0 denominator
return nil, DivideByZero
}
return z.Quo(x, y), nil
case token.REM:
if y.Sign() == 0 {
return nil, DivideByZero
}
return z.Rem(x, y), nil
case token.AND:
return z.And(x, y), nil
case token.OR:
return z.Or(x, y), nil
case token.XOR:
return z.Xor(x, y), nil
case token.SHL:
if y.Sign() < 0 { // negative shift
return nil, NegativeShift
}
return z.Lsh(x, uint(y.Int64())), nil
case token.SHR:
if y.Sign() < 0 {
return nil, NegativeShift
}
return z.Rsh(x, uint(y.Int64())), nil
case token.AND_NOT:
return z.AndNot(x, y), nil
default:
return nil, UnknownOpErr
}
case *ast.UnaryExpr:
var z *big.Int
var err error
if z, err = Eval(nn.X); err != nil {
return nil, err
}
switch nn.Op {
case token.SUB: // -x
return z.Neg(z), nil
case token.XOR: // ^x
return z.Not(z), nil
case token.ADD: // +x (useless)
return z, nil
}
case *ast.BasicLit:
z := new(big.Int)
switch nn.Kind {
case token.INT:
z.SetString(nn.Value, 0)
return z, nil
default:
return nil, UnknownLitErr
}
case *ast.ParenExpr:
z, err := Eval(nn.X)
if err != nil {
return nil, err
}
return z, nil
case *ast.CallExpr:
ident, ok := nn.Fun.(*ast.Ident)
if !ok {
return nil, UnknownTokenErr // quarter to four am; dunno correct error
}
var f Func
f, ok = FuncMap[ident.Name]
if !ok {
return nil, UnknownFuncErr
}
var aerr error
args := make([]*big.Int, len(nn.Args))
for i, a := range nn.Args {
if args[i], aerr = Eval(a); aerr != nil {
return nil, aerr
}
}
x, xerr := f(args...)
if xerr != nil {
return nil, xerr
//.........这里部分代码省略.........