Trait ethers::core::k256::elliptic_curve::ops::Neg 1.0.0[−][src]
Expand description
The unary negation operator -
.
Examples
An implementation of Neg
for Sign
, which allows the use of -
to
negate its value.
use std::ops::Neg;
#[derive(Debug, PartialEq)]
enum Sign {
Negative,
Zero,
Positive,
}
impl Neg for Sign {
type Output = Self;
fn neg(self) -> Self::Output {
match self {
Sign::Negative => Sign::Positive,
Sign::Zero => Sign::Zero,
Sign::Positive => Sign::Negative,
}
}
}
// A negative positive is a negative.
assert_eq!(-Sign::Positive, Sign::Negative);
// A double negative is a positive.
assert_eq!(-Sign::Negative, Sign::Positive);
// Zero is its own negation.
assert_eq!(-Sign::Zero, Sign::Zero);
Associated Types
Required methods
Implementations on Foreign Types
type Output = Saturating<i8>
type Output = Saturating<i16>
type Output = Saturating<i64>
type Output = Saturating<i128>
type Output = Saturating<i32>
type Output = Saturating<isize>
type Output = ScalarCore<C>
type Output = ScalarCore<C>
impl<'a, O, T> Neg for &'a mut BitSlice<O, T> where
O: BitOrder,
T: 'a + BitStore,
impl<'a, O, T> Neg for &'a mut BitSlice<O, T> where
O: BitOrder,
T: 'a + BitStore,
Performs fixed-width 2’s-complement negation of a BitSlice
.
Unlike the !
operator (Not
trait), the unary -
operator treats the
BitSlice
as if it represents a signed 2’s-complement integer of fixed
width. The negation of a number in 2’s complement is defined as its
inversion (using !
) plus one, and on fixed-width numbers has the following
discontinuities:
- A slice whose bits are all zero is considered to represent the number zero which negates as itself.
- A slice whose bits are all one is considered to represent the most negative number, which has no correpsonding positive number, and thus negates as zero.
This behavior was chosen so that all possible values would have some
output, and so that repeated application converges at idempotence. The most
negative input can never be reached by negation, but --MOST_NEG
converges
at the least unreasonable fallback value, 0.
Because BitSlice
cannot move, the negation is performed in place.
Perform 2’s-complement fixed-width negation.
Negation is accomplished by inverting the bits and adding one. This has
one edge case: 1000…
, the most negative number for its width, will
negate to zero instead of itself. It thas no corresponding positive
number to which it can negate.
Parameters
self
Examples
The contortions shown here are a result of this operator applying to a
mutable reference, and this example balancing access to the original
BitVec
for comparison with aquiring a mutable borrow as a slice to
ensure that the BitSlice
implementation is used, not the BitVec
.
Negate an arbitrary positive number (first bit unset).
use bitvec::prelude::*;
let mut src = 0b0110_1010u8;
let bits = src.bits_mut::<Msb0>();
eprintln!("{:?}", bits.split_at(4));
let num = &mut bits[.. 4];
-num;
eprintln!("{:?}", bits.split_at(4));
assert_eq!(&bits[.. 4], &bits[4 ..]);
Negate an arbitrary negative number. This example will use the above result to demonstrate round-trip correctness.
use bitvec::prelude::*;
let mut src = 0b1010_0110u8;
let bits = src.bits_mut::<Msb0>();
let num = &mut bits[.. 4];
-num;
assert_eq!(&bits[.. 4], &bits[4 ..]);
Negate the most negative number, which will become zero, and show convergence at zero.
use bitvec::prelude::*;
let mut src = 128u8;
let bits = src.bits_mut::<Msb0>();
let num = &mut bits[..];
-num;
assert!(bits.not_any());
let num = &mut bits[..];
-num;
assert!(bits.not_any());
type Output = &'a mut BitSlice<O, T>
impl<O, T> Neg for BitBox<O, T> where
O: BitOrder,
T: BitStore,
impl<O, T> Neg for BitBox<O, T> where
O: BitOrder,
T: BitStore,
impl<O, T> Neg for BitVec<O, T> where
O: BitOrder,
T: BitStore,
impl<O, T> Neg for BitVec<O, T> where
O: BitOrder,
T: BitStore,
2’s-complement negation of a BitVec
.
In 2’s-complement, negation is defined as bit-inversion followed by adding one.
Numeric arithmetic is provided on BitVec
as a convenience. Serious numeric
computation on variable-length integers should use the num_bigint
crate
instead, which is written specifically for that use case. BitVec
s are not
intended for arithmetic, and bitvec
makes no guarantees about sustained
correctness in arithmetic at this time.