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
use integer::Integer;
use malachite_base::num::logic::traits::NotAssign;
use natural::Natural;
use platform::Limb;
use std::ops::Not;

// Returns the bitwise not of a slice of limbs.
//
// # Worst-case complexity
// $T(n) = O(n)$
//
// $M(n) = O(n)$
//
// where $T$ is time, $M$ is additional memory, and $n$ is `xs.len()`.
//
// This is equivalent to `mpn_com` from `mpn/generic/com.c`, GMP 6.2.1, where `rp` is returned.
pub_test! {limbs_not(xs: &[Limb]) -> Vec<Limb> {
    xs.iter().map(|x| !x).collect()
}}

// Writes the bitwise not of a slice of limbs to the lowest `x.len()` limbs of `out`. For this to
// work, `out` must be at least as long as `xs`.
//
// # Worst-case complexity
// $T(n) = O(n)$
//
// $M(n) = O(1)$
//
// where $T$ is time, $M$ is additional memory, and $n$ is `xs.len()`.
//
// This is equivalent to `mpn_com` from `mpn/generic/com.c`, GMP 6.2.1, where `rp != up`.
//
// # Panics
// Panics if `out` is shorter than `xs`.
pub_crate_test! {limbs_not_to_out(out: &mut [Limb], xs: &[Limb]) {
    assert!(out.len() >= xs.len());
    for (x, y) in out.iter_mut().zip(xs.iter()) {
        *x = !y;
    }
}}

// Takes the bitwise not of a slice of limbs in place.
//
// # Worst-case complexity
// $T(n) = O(n)$
//
// $M(n) = O(1)$
//
// where $T$ is time, $M$ is additional memory, and $n$ is `xs.len()`.
//
// This is equivalent to `mpn_com` from `mpn/generic/com.c`, GMP 6.2.1, where `rp == up`.
pub_crate_test! {limbs_not_in_place(xs: &mut [Limb]) {
    for x in xs.iter_mut() {
        x.not_assign();
    }
}}

impl Not for Natural {
    type Output = Integer;

    /// Returns the bitwise negation of a [`Natural`], taking it by value and returning an
    /// [`Integer`].
    ///
    /// The [`Natural`] is bitwise-negated as if it were represented in two's complement.
    ///
    /// $$
    /// f(n) = -n - 1.
    /// $$
    ///
    /// # Worst-case complexity
    /// $T(n) = O(n)$
    ///
    /// $M(n) = O(1)$
    ///
    /// where $T$ is time, $M$ is additional memory, and $n$ is `self.significant_bits()`.
    ///
    /// # Examples
    /// ```
    /// extern crate malachite_base;
    ///
    /// use malachite_base::num::basic::traits::Zero;
    /// use malachite_nz::natural::Natural;
    ///
    /// assert_eq!(!Natural::ZERO, -1);
    /// assert_eq!(!Natural::from(123u32), -124);
    /// ```
    fn not(self) -> Integer {
        Integer {
            sign: false,
            abs: self.add_limb(1),
        }
    }
}

impl<'a> Not for &'a Natural {
    type Output = Integer;

    /// Returns the bitwise negation of a [`Natural`], taking it by reference and returning an
    /// [`Integer`].
    ///
    /// The [`Natural`] is bitwise-negated as if it were represented in two's complement.
    ///
    /// $$
    /// f(n) = -n - 1.
    /// $$
    ///
    /// # Worst-case complexity
    /// $T(n) = O(n)$
    ///
    /// $M(n) = O(n)$
    ///
    /// where $T$ is time, $M$ is additional memory, and $n$ is `self.significant_bits()`.
    ///
    /// # Examples
    /// ```
    /// extern crate malachite_base;
    ///
    /// use malachite_base::num::basic::traits::Zero;
    /// use malachite_nz::natural::Natural;
    ///
    /// assert_eq!(!&Natural::ZERO, -1);
    /// assert_eq!(!&Natural::from(123u32), -124);
    /// ```
    fn not(self) -> Integer {
        Integer {
            sign: false,
            abs: self.add_limb_ref(1),
        }
    }
}