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
131
use crate::integer::Integer;
use crate::natural::Natural;
use malachite_base::num::basic::traits::One;
use malachite_base::num::logic::traits::NotAssign;
use std::ops::Not;

impl Not for Integer {
    type Output = Integer;

    /// Returns the bitwise negation of an [`Integer`], taking it by value.
    ///
    /// $$
    /// 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::integer::Integer;
    ///
    /// assert_eq!(!Integer::ZERO, -1);
    /// assert_eq!(!Integer::from(123), -124);
    /// assert_eq!(!Integer::from(-123), 122);
    /// ```
    #[inline]
    fn not(mut self) -> Integer {
        self.not_assign();
        self
    }
}

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

    /// Returns the bitwise negation of an [`Integer`], taking it by reference.
    ///
    /// $$
    /// 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::integer::Integer;
    ///
    /// assert_eq!(!&Integer::ZERO, -1);
    /// assert_eq!(!&Integer::from(123), -124);
    /// assert_eq!(!&Integer::from(-123), 122);
    /// ```
    fn not(self) -> Integer {
        match *self {
            Integer {
                sign: true,
                ref abs,
            } => Integer {
                sign: false,
                abs: abs.add_limb_ref(1),
            },
            Integer {
                sign: false,
                ref abs,
            } => Integer {
                sign: true,
                abs: abs.sub_limb_ref(1),
            },
        }
    }
}

impl NotAssign for Integer {
    /// Replaces an [`Integer`] with its bitwise negation.
    ///
    /// $$
    /// n \gets -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_base::num::logic::traits::NotAssign;
    /// use malachite_nz::integer::Integer;
    ///
    /// let mut x = Integer::ZERO;
    /// x.not_assign();
    /// assert_eq!(x, -1);
    ///
    /// let mut x = Integer::from(123);
    /// x.not_assign();
    /// assert_eq!(x, -124);
    ///
    /// let mut x = Integer::from(-123);
    /// x.not_assign();
    /// assert_eq!(x, 122);
    /// ```
    fn not_assign(&mut self) {
        if self.sign {
            self.sign = false;
            self.abs += Natural::ONE;
        } else {
            self.sign = true;
            self.abs -= Natural::ONE;
        }
    }
}