oxinum-int 0.1.1

Arbitrary-precision integers for OxiNum (UBig/IBig via dashu-int)
Documentation
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
//! Two's-complement bitwise operations on [`BigInt`]:
//! `BitAnd`/`BitOr`/`BitXor`/`Not`/`Shl`/`Shr`.
//!
//! # Two's-complement semantics
//!
//! `BigInt` represents an infinite-precision signed integer. For bitwise
//! operations we use the mathematical two's-complement model: every value has
//! an infinite conceptual bit string. Positive numbers are padded with infinite
//! zeros on the left; negative numbers are padded with infinite ones (their
//! mathematical two's-complement of their magnitude never terminates).
//!
//! ## Algorithm overview
//!
//! For `&`, `|`, `^` on `BigInt`:
//! 1. Choose `nlimbs = max(|a|.limbs, |b|.limbs) + 1` (the extra limb absorbs
//!    the sign-extension and prevents the result from crossing back through
//!    the sign boundary for most cases).
//! 2. Convert both operands to their two's-complement limb vectors of length
//!    `nlimbs` via [`to_twos_complement`].
//! 3. Apply the operation limb-wise.
//! 4. Decode the result back to a `BigInt` via [`from_twos_complement`].
//!
//! ## `Not`
//!
//! The mathematical rule is `!x = -(x) - 1`:
//! - `!0 = -1`
//! - `!5 = -6`
//! - `!(-1) = 0`
//! - `!(-6) = 5`
//!
//! ## Arithmetic right shift
//!
//! `>>` is **arithmetic** (sign-extending) shift — it equals floor division by
//! `2^k`. For `k >= 64*limbs` the result is `0` (positive) or `-1` (negative).
//!
//! For negative `self = -m` (m > 0):
//! ```text
//! floor(-m / 2^k) = -ceil(m / 2^k) = -(((m - 1) >> k) + 1)
//! ```

use super::int::BigInt;
use super::uint::BigUint;
use core::ops::{BitAnd, BitAndAssign, BitOr, BitOrAssign, BitXor, BitXorAssign, Not, Shl, Shr};
use oxinum_core::Sign;

// ---------------------------------------------------------------------------
// Two's-complement helpers
// ---------------------------------------------------------------------------

/// Convert `n` to its two's-complement representation as a `Vec<u64>` of
/// exactly `nlimbs` limbs (little-endian).
///
/// - `n >= 0`: copy `n.magnitude().as_limbs()`, zero-pad to `nlimbs`.
/// - `n < 0` (value = `-m`, m > 0): pad `m` to `nlimbs`, then negate in
///   two's complement (flip all bits, add 1 with carry).
fn to_twos_complement(n: &BigInt, nlimbs: usize) -> Vec<u64> {
    let mag = n.magnitude().as_limbs();
    let mut out = vec![0u64; nlimbs];
    // Copy the magnitude limbs (they fit because nlimbs >= mag.len()).
    let copy_len = mag.len().min(nlimbs);
    out[..copy_len].copy_from_slice(&mag[..copy_len]);

    if n.is_negative() {
        // Negate in two's complement: flip all bits then add 1.
        for limb in out.iter_mut() {
            *limb = !*limb;
        }
        // Add 1 with carry propagation.
        let mut carry: u64 = 1;
        for limb in out.iter_mut() {
            let (v, c) = limb.overflowing_add(carry);
            *limb = v;
            carry = c as u64;
            if carry == 0 {
                break;
            }
        }
        // Any remaining carry means we overflowed nlimbs; since the
        // true mathematical value is exact, this can only happen if
        // m == 0 (which is forbidden for negative BigInt by the canonical-zero
        // invariant) or if nlimbs is absurdly small.
    }
    out
}

/// Decode a two's-complement limb vector (little-endian) back to a `BigInt`.
///
/// Sign is determined by the MSB of `limbs[n-1]`. For a positive result,
/// the limbs are directly the magnitude. For a negative result, the magnitude
/// is recovered by two's-complement negation of the limb vector.
fn from_twos_complement(limbs: &[u64]) -> BigInt {
    if limbs.is_empty() {
        return BigInt::zero();
    }
    let top = limbs[limbs.len() - 1];
    if top >> 63 == 0 {
        // Non-negative: the limbs *are* the magnitude.
        BigInt::from_parts(Sign::Positive, BigUint::from_le_limbs(limbs))
    } else {
        // Negative: recover magnitude via two's-complement negation.
        let mut neg: Vec<u64> = limbs.to_vec();
        for v in neg.iter_mut() {
            *v = !*v;
        }
        let mut carry: u64 = 1;
        for v in neg.iter_mut() {
            let (nv, c) = v.overflowing_add(carry);
            *v = nv;
            carry = c as u64;
            if carry == 0 {
                break;
            }
        }
        BigInt::from_parts(Sign::Negative, BigUint::from_le_limbs(&neg))
    }
}

// ---------------------------------------------------------------------------
// Core binary-op helper
// ---------------------------------------------------------------------------

/// Apply a bitwise binary op to two `BigInt` values under two's-complement.
#[inline]
fn bigint_binop<F>(lhs: &BigInt, rhs: &BigInt, op: F) -> BigInt
where
    F: Fn(u64, u64) -> u64,
{
    let llen = lhs.magnitude().as_limbs().len();
    let rlen = rhs.magnitude().as_limbs().len();
    // Extra limb absorbs sign-extension noise for all finite pairs.
    let nlimbs = llen.max(rlen) + 1;
    let ltc = to_twos_complement(lhs, nlimbs);
    let rtc = to_twos_complement(rhs, nlimbs);
    let result: Vec<u64> = ltc
        .iter()
        .zip(rtc.iter())
        .map(|(&a, &b)| op(a, b))
        .collect();
    from_twos_complement(&result)
}

// ---------------------------------------------------------------------------
// BitAnd for BigInt
// ---------------------------------------------------------------------------

impl BitAnd<&BigInt> for &BigInt {
    type Output = BigInt;
    #[inline]
    fn bitand(self, rhs: &BigInt) -> BigInt {
        bigint_binop(self, rhs, |a, b| a & b)
    }
}

impl BitAnd<BigInt> for BigInt {
    type Output = BigInt;
    #[inline]
    fn bitand(self, rhs: BigInt) -> BigInt {
        bigint_binop(&self, &rhs, |a, b| a & b)
    }
}

impl BitAnd<&BigInt> for BigInt {
    type Output = BigInt;
    #[inline]
    fn bitand(self, rhs: &BigInt) -> BigInt {
        bigint_binop(&self, rhs, |a, b| a & b)
    }
}

impl BitAnd<BigInt> for &BigInt {
    type Output = BigInt;
    #[inline]
    fn bitand(self, rhs: BigInt) -> BigInt {
        bigint_binop(self, &rhs, |a, b| a & b)
    }
}

// ---------------------------------------------------------------------------
// BitOr for BigInt
// ---------------------------------------------------------------------------

impl BitOr<&BigInt> for &BigInt {
    type Output = BigInt;
    #[inline]
    fn bitor(self, rhs: &BigInt) -> BigInt {
        bigint_binop(self, rhs, |a, b| a | b)
    }
}

impl BitOr<BigInt> for BigInt {
    type Output = BigInt;
    #[inline]
    fn bitor(self, rhs: BigInt) -> BigInt {
        bigint_binop(&self, &rhs, |a, b| a | b)
    }
}

impl BitOr<&BigInt> for BigInt {
    type Output = BigInt;
    #[inline]
    fn bitor(self, rhs: &BigInt) -> BigInt {
        bigint_binop(&self, rhs, |a, b| a | b)
    }
}

impl BitOr<BigInt> for &BigInt {
    type Output = BigInt;
    #[inline]
    fn bitor(self, rhs: BigInt) -> BigInt {
        bigint_binop(self, &rhs, |a, b| a | b)
    }
}

// ---------------------------------------------------------------------------
// BitXor for BigInt
// ---------------------------------------------------------------------------

impl BitXor<&BigInt> for &BigInt {
    type Output = BigInt;
    #[inline]
    fn bitxor(self, rhs: &BigInt) -> BigInt {
        bigint_binop(self, rhs, |a, b| a ^ b)
    }
}

impl BitXor<BigInt> for BigInt {
    type Output = BigInt;
    #[inline]
    fn bitxor(self, rhs: BigInt) -> BigInt {
        bigint_binop(&self, &rhs, |a, b| a ^ b)
    }
}

impl BitXor<&BigInt> for BigInt {
    type Output = BigInt;
    #[inline]
    fn bitxor(self, rhs: &BigInt) -> BigInt {
        bigint_binop(&self, rhs, |a, b| a ^ b)
    }
}

impl BitXor<BigInt> for &BigInt {
    type Output = BigInt;
    #[inline]
    fn bitxor(self, rhs: BigInt) -> BigInt {
        bigint_binop(self, &rhs, |a, b| a ^ b)
    }
}

// ---------------------------------------------------------------------------
// Not for BigInt  — !x = -(x) - 1
// ---------------------------------------------------------------------------

impl Not for BigInt {
    type Output = BigInt;

    fn not(self) -> BigInt {
        if self.is_negative() {
            // self = -m (m > 0); !self = m - 1 (= -(-m) - 1 = m - 1 >= 0)
            let m = self.magnitude().clone();
            let one = BigUint::one();
            match m.checked_sub(&one) {
                Some(result) => BigInt::from_parts(Sign::Positive, result),
                // m was 1 (i.e., self = -1), so m - 1 = 0.
                None => BigInt::zero(),
            }
        } else {
            // self >= 0; !self = -(self + 1) = -(self.mag + 1)
            let mag_plus_one = self.magnitude() + &BigUint::one();
            BigInt::from_parts(Sign::Negative, mag_plus_one)
        }
    }
}

impl Not for &BigInt {
    type Output = BigInt;

    #[inline]
    fn not(self) -> BigInt {
        self.clone().not()
    }
}

// ---------------------------------------------------------------------------
// Shl for BigInt — left shift: sign preserved, magnitude shifted left
// ---------------------------------------------------------------------------

impl Shl<u64> for BigInt {
    type Output = BigInt;

    fn shl(self, k: u64) -> BigInt {
        if self.is_zero() || k == 0 {
            return self;
        }
        let sign = self.sign();
        let shifted_mag = self.magnitude().shl_bits(k);
        BigInt::from_parts(sign, shifted_mag)
    }
}

impl Shl<u64> for &BigInt {
    type Output = BigInt;

    #[inline]
    fn shl(self, k: u64) -> BigInt {
        self.clone().shl(k)
    }
}

// ---------------------------------------------------------------------------
// Shr for BigInt — ARITHMETIC right shift (floor division by 2^k)
//
// For self >= 0: equivalent to BigUint shr (logical shift on magnitude).
// For self = -m (m > 0): floor(-m / 2^k) = -(((m - 1) >> k) + 1)
// ---------------------------------------------------------------------------

impl Shr<u64> for BigInt {
    type Output = BigInt;

    fn shr(self, k: u64) -> BigInt {
        if k == 0 {
            return self;
        }
        if self.is_negative() {
            let m = self.magnitude().clone();
            // (m - 1) >> k; if k >= bit_length(m - 1) the result is 0.
            let m_minus_one = m.checked_sub(&BigUint::one()).unwrap_or_else(BigUint::zero);
            let shifted = m_minus_one.shr_bits(k);
            // Result = -(shifted + 1)
            let mag = &shifted + &BigUint::one();
            BigInt::from_parts(Sign::Negative, mag)
        } else {
            // Non-negative: logical shift on magnitude.
            BigInt::from_parts(Sign::Positive, self.magnitude().shr_bits(k))
        }
    }
}

impl Shr<u64> for &BigInt {
    type Output = BigInt;

    #[inline]
    fn shr(self, k: u64) -> BigInt {
        self.clone().shr(k)
    }
}

// ---------------------------------------------------------------------------
// Internal unit tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;

    fn bi(v: i64) -> BigInt {
        BigInt::from(v)
    }

    fn bu(v: u64) -> BigUint {
        BigUint::from_u64(v)
    }

    #[test]
    fn not_basic() {
        assert_eq!(!bi(0), bi(-1));
        assert_eq!(!bi(5), bi(-6));
        assert_eq!(!bi(-1), bi(0));
        assert_eq!(!bi(-6), bi(5));
    }

    #[test]
    fn not_double_negation() {
        for v in [-1000i64, -1, 0, 1, 1000] {
            let n = bi(v);
            assert_eq!(!!n.clone(), n, "!!n == n failed for {v}");
        }
    }

    #[test]
    fn and_neg_one_with_ff() {
        // -1 in two's complement has all bits 1; -1 & x == x.
        let neg1 = bi(-1);
        let ff = bi(0xFF);
        assert_eq!(&neg1 & &ff, ff);
    }

    #[test]
    fn or_neg_one_is_neg_one() {
        let neg1 = bi(-1);
        let ff = bi(0xFF);
        assert_eq!(&neg1 | &ff, neg1);
    }

    #[test]
    fn xor_self_is_zero() {
        let neg1 = bi(-1);
        assert_eq!(&neg1 ^ &neg1, BigInt::zero());
        let x = bi(-12345);
        assert_eq!(&x ^ &x, BigInt::zero());
    }

    #[test]
    fn shr_negative_floor_div() {
        assert_eq!(bi(-8) >> 1u64, bi(-4));
        assert_eq!(bi(-7) >> 1u64, bi(-4));
        assert_eq!(bi(-1) >> 1u64, bi(-1));
        assert_eq!(bi(-1) >> 100u64, bi(-1));
        assert_eq!(bi(7) >> 1u64, bi(3));
    }

    #[test]
    fn shl_signed() {
        assert_eq!(bi(1) << 4u64, bi(16));
        assert_eq!(bi(-1) << 4u64, bi(-16));
        assert_eq!(bi(0) << 100u64, BigInt::zero());
    }

    #[test]
    fn to_twos_complement_positive() {
        let n = bi(5);
        let tc = to_twos_complement(&n, 2);
        assert_eq!(tc, vec![5, 0]);
    }

    #[test]
    fn to_twos_complement_negative_one() {
        let n = bi(-1);
        let tc = to_twos_complement(&n, 2);
        // -1 in two's complement: all bits 1.
        assert_eq!(tc, vec![u64::MAX, u64::MAX]);
    }

    #[test]
    fn from_twos_complement_roundtrip() {
        for v in [-1i128, -2, -128, 0, 1, 127, 1000] {
            let n = BigInt::from(v);
            let tc = to_twos_complement(&n, 4);
            let back = from_twos_complement(&tc);
            assert_eq!(back, n, "roundtrip failed for {v}");
        }
    }

    #[test]
    fn de_morgan_and_to_or() {
        // !(a & b) == !a | !b
        let pairs: &[(i64, i64)] = &[
            (0, 0),
            (5, 3),
            (-5, 3),
            (5, -3),
            (-5, -3),
            (-1, 0xFF),
            (i64::MAX, i64::MIN),
        ];
        for &(av, bv) in pairs {
            let a = bi(av);
            let b = bi(bv);
            let lhs = !(&a & &b);
            let rhs = !a.clone() | !b.clone();
            assert_eq!(lhs, rhs, "De Morgan failed for ({av}, {bv})");
        }
    }

    #[test]
    fn de_morgan_or_to_and() {
        // !(a | b) == !a & !b
        let pairs: &[(i64, i64)] = &[(0, 0), (5, 3), (-5, 3), (5, -3), (-5, -3), (-1, 0xFF)];
        for &(av, bv) in pairs {
            let a = bi(av);
            let b = bi(bv);
            let lhs = !(&a | &b);
            let rhs = !a.clone() & !b.clone();
            assert_eq!(lhs, rhs, "De Morgan (or→and) failed for ({av}, {bv})");
        }
    }

    #[test]
    fn i128_cross_val_bitwise_and_shifts() {
        let vals: &[i128] = &[
            0,
            1,
            -1,
            127,
            -128,
            1000,
            -1000,
            i64::MAX as i128,
            i64::MIN as i128,
        ];
        for &i in vals {
            let a = BigInt::from(i);
            // NOT
            assert_eq!(!a.clone(), BigInt::from(!i), "!{i} mismatch");
            // Shifts with small non-negative j
            for j in 0i128..20 {
                let b = BigInt::from(j);
                assert_eq!(&a & &b, BigInt::from(i & j), "{i} & {j} mismatch");
                assert_eq!(&a | &b, BigInt::from(i | j), "{i} | {j} mismatch");
                assert_eq!(&a ^ &b, BigInt::from(i ^ j), "{i} ^ {j} mismatch");
                assert_eq!(
                    a.clone() >> (j as u64),
                    BigInt::from(i >> j),
                    "{i} >> {j} mismatch"
                );
            }
            for &j in vals {
                assert_eq!(
                    &a & &BigInt::from(j),
                    BigInt::from(i & j),
                    "{i} & {j} mismatch"
                );
                assert_eq!(
                    &a | &BigInt::from(j),
                    BigInt::from(i | j),
                    "{i} | {j} mismatch"
                );
                assert_eq!(
                    &a ^ &BigInt::from(j),
                    BigInt::from(i ^ j),
                    "{i} ^ {j} mismatch"
                );
            }
        }
    }

    #[test]
    fn xor_identity_and_complement() {
        // x ^ 0 == x
        for v in [-5i64, -1, 0, 1, 5] {
            let n = bi(v);
            assert_eq!(&n ^ &BigInt::zero(), n.clone());
            // x ^ x == 0
            assert_eq!(&n ^ &n, BigInt::zero());
            // x ^ -1 == !x  (since -1 is all-ones in two's complement)
            let all_ones = bi(-1);
            assert_eq!(&n ^ &all_ones, !n.clone());
        }
    }

    #[test]
    fn shr_positive_matches_biguint_shr() {
        let mag = BigUint::from_u64(1234567890);
        let n = BigInt::from_parts(Sign::Positive, mag.clone());
        for k in [0u64, 1, 7, 15, 31, 63, 64, 65] {
            let expected = BigInt::from_parts(Sign::Positive, mag.shr_bits(k));
            assert_eq!(n.clone() >> k, expected, "positive shr mismatch for k={k}");
        }
    }

    #[test]
    fn bu_unused() {
        // Ensure the bu() helper doesn't trigger unused-function warnings by
        // using it in at least one test.
        assert_eq!(bu(42), BigUint::from_u64(42));
    }
}

// ---------------------------------------------------------------------------
// BitAndAssign / BitOrAssign / BitXorAssign for native::BigUint
//
// The core BitAnd/BitOr/BitXor impls (4 ref-combinations each) live in
// `uint.rs` and are wired to call `simd_ops` kernels.  Here we add only the
// assign variants, keeping bitwise-op code grouped in this file.
// ---------------------------------------------------------------------------

impl BitAndAssign<BigUint> for BigUint {
    #[inline]
    fn bitand_assign(&mut self, rhs: BigUint) {
        *self = &*self & &rhs;
    }
}

impl BitAndAssign<&BigUint> for BigUint {
    #[inline]
    fn bitand_assign(&mut self, rhs: &BigUint) {
        *self = &*self & rhs;
    }
}

impl BitOrAssign<BigUint> for BigUint {
    #[inline]
    fn bitor_assign(&mut self, rhs: BigUint) {
        *self = &*self | &rhs;
    }
}

impl BitOrAssign<&BigUint> for BigUint {
    #[inline]
    fn bitor_assign(&mut self, rhs: &BigUint) {
        *self = &*self | rhs;
    }
}

impl BitXorAssign<BigUint> for BigUint {
    #[inline]
    fn bitxor_assign(&mut self, rhs: BigUint) {
        *self = &*self ^ &rhs;
    }
}

impl BitXorAssign<&BigUint> for BigUint {
    #[inline]
    fn bitxor_assign(&mut self, rhs: &BigUint) {
        *self = &*self ^ rhs;
    }
}

// ---------------------------------------------------------------------------
// Unit tests for BigUint bitwise ops
// ---------------------------------------------------------------------------

#[cfg(test)]
mod biguint_bitwise_tests {
    use super::*;

    fn bu(v: u64) -> BigUint {
        BigUint::from_u64(v)
    }

    // ------- AND -------

    #[test]
    fn and_basic() {
        assert_eq!(&bu(0b1100) & &bu(0b1010), bu(0b1000));
        assert_eq!(&bu(0xFF) & &bu(0x0F), bu(0x0F));
        assert_eq!(&bu(0) & &bu(0xFF), bu(0));
    }

    #[test]
    fn and_zero_identity() {
        let x = bu(0xDEAD_BEEF);
        assert_eq!(&x & &bu(0), bu(0), "a & 0 == 0");
    }

    #[test]
    fn and_self_identity() {
        let x = bu(0xDEAD_BEEF);
        assert_eq!(&x & &x, x.clone(), "a & a == a");
    }

    #[test]
    fn and_unequal_limbs() {
        // a has 2 limbs, b has 1 limb: result should only be 1 limb (the AND of
        // the overlap; higher limbs of a vanish because AND with 0 is 0).
        let mut limbs_a = vec![0xFFFF_FFFF_FFFF_FFFFu64, 0xFFFF_FFFF_FFFF_FFFFu64];
        let limbs_b = vec![0xAAAA_AAAA_AAAA_AAAAu64];
        let a = BigUint::from_le_limbs(&limbs_a);
        let b = BigUint::from_le_limbs(&limbs_b);
        let result = &a & &b;
        assert_eq!(result, BigUint::from_le_limbs(&limbs_b));
        // Confirm normalization: a & 0 in higher limbs yields no trailing zeros
        limbs_a[1] = 0;
        let a2 = BigUint::from_le_limbs(&limbs_a);
        let result2 = &a2 & &b;
        assert_eq!(result2, BigUint::from_le_limbs(&limbs_b));
    }

    #[test]
    fn and_assign() {
        let mut x = bu(0b1111);
        x &= bu(0b1010);
        assert_eq!(x, bu(0b1010));
    }

    // ------- OR -------

    #[test]
    fn or_basic() {
        assert_eq!(&bu(0b1100) | &bu(0b1010), bu(0b1110));
        assert_eq!(&bu(0xF0) | &bu(0x0F), bu(0xFF));
    }

    #[test]
    fn or_zero_identity() {
        let x = bu(0xDEAD_BEEF);
        assert_eq!(&x | &bu(0), x.clone(), "a | 0 == a");
    }

    #[test]
    fn or_self_identity() {
        let x = bu(0xDEAD_BEEF);
        assert_eq!(&x | &x, x.clone(), "a | a == a");
    }

    #[test]
    fn or_unequal_limbs() {
        let limbs_a = vec![0x1111_1111_1111_1111u64, 0x2222_2222_2222_2222u64];
        let limbs_b = vec![0x4444_4444_4444_4444u64];
        let a = BigUint::from_le_limbs(&limbs_a);
        let b = BigUint::from_le_limbs(&limbs_b);
        let result = &a | &b;
        // Lower limb: OR, upper limb: preserved from a
        let expected = BigUint::from_le_limbs(&[
            0x1111_1111_1111_1111u64 | 0x4444_4444_4444_4444u64,
            0x2222_2222_2222_2222u64,
        ]);
        assert_eq!(result, expected);
    }

    #[test]
    fn or_assign() {
        let mut x = bu(0b1010);
        x |= bu(0b0101);
        assert_eq!(x, bu(0b1111));
    }

    // ------- XOR -------

    #[test]
    fn xor_basic() {
        assert_eq!(&bu(0b1100) ^ &bu(0b1010), bu(0b0110));
        assert_eq!(&bu(0xFF) ^ &bu(0x0F), bu(0xF0));
    }

    #[test]
    fn xor_self_is_zero() {
        let x = bu(0xDEAD_BEEF_CAFE_BABEu64);
        assert_eq!(&x ^ &x, bu(0), "a ^ a == 0");
    }

    #[test]
    fn xor_zero_identity() {
        let x = bu(0xDEAD_BEEF);
        assert_eq!(&x ^ &bu(0), x.clone(), "a ^ 0 == a");
    }

    #[test]
    fn xor_unequal_limbs() {
        let limbs_a = vec![0xFFFF_FFFF_FFFF_FFFFu64, 0xFFFF_FFFF_FFFF_FFFFu64];
        let limbs_b = vec![0xFFFF_FFFF_FFFF_FFFFu64];
        let a = BigUint::from_le_limbs(&limbs_a);
        let b = BigUint::from_le_limbs(&limbs_b);
        let result = &a ^ &b;
        // Lower limb XOR cancels to 0; upper limb is 0xFFFF... ^ 0 = 0xFFFF...
        let expected = BigUint::from_le_limbs(&[0u64, 0xFFFF_FFFF_FFFF_FFFFu64]);
        assert_eq!(result, expected);
    }

    #[test]
    fn xor_double_is_identity() {
        // a ^ b ^ b == a
        let a = BigUint::from_le_limbs(&[0xDEAD_BEEF, 0xCAFE_BABE, 0x1234_5678]);
        let b = BigUint::from_le_limbs(&[0x1111_2222, 0x3333_4444]);
        let c = &(&a ^ &b) ^ &b;
        assert_eq!(c, a);
    }

    #[test]
    fn xor_normalization() {
        // XOR of equal numbers must normalize to zero (empty limbs).
        let big = BigUint::from_le_limbs(&[
            0xAAAA_BBBB_CCCC_DDDDu64,
            0xEEEE_FFFF_0000_1111u64,
            0x2222_3333_4444_5555u64,
        ]);
        let result: BigUint = &big ^ &big;
        assert_eq!(result, BigUint::zero());
        assert!(result.is_zero());
    }

    #[test]
    fn xor_assign() {
        let mut x = bu(0b1111);
        x ^= bu(0b1010);
        assert_eq!(x, bu(0b0101));
    }

    // ------- owned variants compile and produce correct results -------

    #[test]
    fn owned_ops() {
        let a = bu(0b1110);
        let b = bu(0b1011);
        assert_eq!(a.clone() & b.clone(), bu(0b1010));
        assert_eq!(a.clone() | b.clone(), bu(0b1111));
        assert_eq!(a.clone() ^ b.clone(), bu(0b0101));
    }
}