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
//! Bitwise operations on fixed-size, arbitrary big buffer of bytes.
//!
//! The primitive types `u8`, `u16`, `u32` and `u64` are very useful types,
//! when one needs to perform boolean algebra on many bits at once (bitwise
//! operations).
//!
//! This crate complements these primitive types, with subsequent power-of-two
//! sizes: `Bw128`, `Bw256`, etc. These types are all `Copy` (that is, they can
//! be trivially copied as raw memory), and their size is really the size given
//! by their names (`Bw256` takes 256 bits). You may be quickly limited by
//! Rust's default stack size if you store these types directly on the stack.
//! Don't forget to box your values if you want them to live on the heap!
//!
//! If the types provided are not enough, you can easily define your own by
//! creating an alias to a `BwPair<X>`. Only power-of-two sizes are supported.

extern crate rand;

#[cfg(test)]
extern crate quickcheck;

pub mod bigwise {

    use rand::{Rand, Rng};
    use std::fmt::{self, Debug, Formatter};

    /// A type that supports bitwise operations.
    ///
    /// An impl of this trait must declare the number of bits it woks with,
    /// using the `size` method.
    ///
    /// Note that bitwise operations can be applied only between operands of
    /// the exact same type, and not two different types that both implement
    /// `Bigwise`.
    ///
    pub trait Bigwise : Rand + Debug + Eq {

        /// Number of bits stored.
        fn size() -> u32;

        /// A value with all bits set to 0.
        fn empty() -> Self;

        /// A value with all bits set to 1.
        fn full() -> Self;

        /// Creates a value based on raw data.
        ///
        /// The LSB (least significant bit) of the last element of the slice
        /// will become the LSB of the created value.
        ///
        /// If `byte` contains less bits than the type allows, then the MSB
        /// (most significant bits) of the created value will be set to 0. If
        /// `byte` contains more bits than the type allows, then only the last
        /// n elements of the slice will be used to set the content of the
        /// created value (with `n` = `size` / 8).
        ///
        fn from_bytes(byte: &[u8]) -> Self;

        /// The content of the value as raw bytes.
        ///
        /// The MSB (most significant bit) of the first u8 is the MSB of the
        /// value. The LSB (least significant bit) of the last u8 is the LSB of
        /// the value.
        ///
        fn to_bytes(&self) -> Vec<u8>;

        /// The boolean value of the given bit.
        ///
        /// Bits are indexed from the least significant (index 0) to the most
        /// significant.
        ///
        /// if `i` is outside the capacity of this type, the returned value is
        /// always false (as if the value had an infinite amout of 0 before
        /// the most significant bit).
        ///
        fn get(&self, i: u32) -> bool;

        /// Assigns a boolean value to a given bit.
        ///
        /// Bits are indexed from the least significant (index 0) to the most
        /// significant.
        ///
        /// # Panics
        ///
        /// Panics if `i` >= `size`.
        ///
        fn set(&mut self, i: u32, v: bool);

        /// An iterator over the bits.
        ///
        /// Bits are iterated from the least significant one to the most
        /// significant one.
        ///
        fn iter(&self) -> BitsIter<Self>;

        /// Shifts all the bits `n` positions to the left.
        ///
        /// The bits that are inserted on the LSB (least significant bit) side
        /// are all zeroes.
        ///
        /// If `n` >= `size`, the result will contain only zeroes.
        ///
        fn shift_left(&self, n: u32) -> Self;

        /// Shifts all the bits `n` positions to the right.
        ///
        /// The bits that are inserted on the MSB (most significant bit) side
        /// are all zeroes.
        ///
        /// If `n` >= `size`, the result will contain only zeroes.
        ///
        fn shift_right(&self, n: u32) -> Self;

        /// Bitwise OR operation
        fn or(&self, rhs: &Self) -> Self;

        /// Bitwise AND operation
        fn and(&self, rhs: &Self) -> Self;

        /// Bitwise XOR operation
        fn xor(&self, rhs: &Self) -> Self;

        /// Bitwise NOT operation
        fn not(&self) -> Self;

        /// Rotates all the bits `n` positions to the left.
        ///
        /// The bits that are lost on the MSB (most significant bit) side are
        /// re-inserted on the LSB (least significant bit) side.
        ///
        fn rotate_left(&self, n: u32) -> Self;

        /// Rotates all the bits `n` positions to the right.
        ///
        /// The bits that are lost on the LSB (least significant bit) side are
        /// re-inserted on the MSB (most significant bit) side.
        ///
        fn rotate_right(&self, n: u32) -> Self;
    }

    /// An iterator over the bits.
    ///
    /// Bits are iterated from the least significant one to the most
    /// significant one.
    ///
    pub struct BitsIter<'a, T: Bigwise + 'a> {
        bw: &'a T,
        idx: u32,
    }

    impl<'a, T: Bigwise> Iterator for BitsIter<'a, T> {
        type Item = bool;

        fn next(&mut self) -> Option<Self::Item> {
            if self.idx < T::size() {
                let res = Some(self.bw.get(self.idx));
                self.idx += 1;
                res
            } else {
                None
            }
        }
    }

    /// A `Bigwise` type composed of 64 bits.
    ///
    /// Wraps a `u64`
    ///
    #[derive(Copy, Clone, Eq, PartialEq)]
    pub struct Bw64(u64);

    impl Bigwise for Bw64 {

        fn size() -> u32 {
            64
        }

        fn empty() -> Self {
            Bw64(0)
        }

        fn full() -> Self {
            Bw64(!0)
        }

        fn from_bytes(bytes: &[u8]) -> Self {
            let mut val = 0u64;
            for (i, &b) in bytes.iter().rev().take(8).enumerate() {
                val |= (b as u64) << (8*i);
            }
            Bw64(val)
        }

        fn to_bytes(&self) -> Vec<u8> {
            let nb_bytes = (Self::size() / 8) as usize;
            let mut res = Vec::with_capacity(nb_bytes);
            for i in (0..nb_bytes).rev() {
                res.push(((self.0 >> (8*i)) & 0xFF) as u8);
            }
            res
        }

        fn get(&self, i: u32) -> bool {
            if i >= Self::size() {
                false
            } else {
                (self.0 >> i) & 1 > 0
            }
        }

        fn set(&mut self, i: u32, v: bool) {
            if i >= Self::size() {
                panic!("index overflow");
            }
            if v {
                self.0 |= 1 << i;
            } else {
                self.0 &= !(1 << i);
            }
        }

        fn iter(&self) -> BitsIter<Self> {
            BitsIter {bw: self, idx: 0}
        }

        fn shift_left(&self, n: u32) -> Self {
            if n >= Self::size() {
                Self::empty()
            } else {
                Bw64(self.0 << n)
            }
        }

        fn shift_right(&self, n: u32) -> Self {
            if n >= Self::size() {
                Self::empty()
            } else {
                Bw64(self.0 >> n)
            }
        }

        fn or(&self, rhs: &Self) -> Self {
            Bw64(self.0 | rhs.0)
        }

        fn and(&self, rhs: &Self) -> Self {
            Bw64(self.0 & rhs.0)
        }

        fn xor(&self, rhs: &Self) -> Self {
            Bw64(self.0 ^ rhs.0)
        }

        fn not(&self) -> Self {
            Bw64(!self.0)
        }

        fn rotate_left(&self, n: u32) -> Self {
            Bw64(self.0.rotate_left(n))
        }

        fn rotate_right(&self, n: u32) -> Self {
            Bw64(self.0.rotate_right(n))
        }
    }

    impl Rand for Bw64 {
        fn rand<R: Rng>(rng: &mut R) -> Self {
            Bw64(rng.next_u64())
        }
    }

    /// The sequence of zeroes and ones, the MSB (most significant bit)
    /// appearing on the left.
    ///
    impl Debug for Bw64 {
        fn fmt(&self, f: &mut Formatter) -> Result<(), fmt::Error> {
            let bytes = self.to_bytes();
            for b in bytes {
                try!(f.write_str(&format!("{:08b}", b)));
            }
            Ok(())
        }
    }

    /// Combines two `Bigwise` to form a bigger one.
    ///
    /// This is the mechanism that allows us to build any power-of-two sized
    /// Bigwise.
    ///
    #[derive(Copy, Clone, Eq, PartialEq)]
    pub struct BwPair<T> where T: Bigwise {
        left: T,
        right: T,
    }

    /// A `Bigwise` type composed of 128 bits.
    pub type Bw128 = BwPair<Bw64>;

    /// A `Bigwise` type composed of 256 bits.
    pub type Bw256 = BwPair<Bw128>;

    /// A `Bigwise` type composed of 512 bits.
    pub type Bw512 = BwPair<Bw256>;

    /// A `Bigwise` type composed of 1024 bits.
    pub type Bw1k = BwPair<Bw512>;

    /// A `Bigwise` type composed of 2.048 bits.
    pub type Bw2k = BwPair<Bw1k>;

    /// A `Bigwise` type composed of 4.096 bits.
    pub type Bw4k = BwPair<Bw2k>;

    /// A `Bigwise` type composed of 8.192 bits.
    pub type Bw8k = BwPair<Bw4k>;

    /// A `Bigwise` type composed of 16.384 bits.
    pub type Bw16k = BwPair<Bw8k>;

    /// A `Bigwise` type composed of 32.768 bits.
    pub type Bw32k = BwPair<Bw16k>;

    /// A `Bigwise` type composed of 65.536 bits.
    pub type Bw64k = BwPair<Bw32k>;

    /// A `Bigwise` type composed of 131.072 bits.
    pub type Bw128k = BwPair<Bw64k>;

    /// A `Bigwise` type composed of 262.144 bits.
    pub type Bw256k = BwPair<Bw128k>;

    /// A `Bigwise` type composed of 524.288 bits.
    pub type Bw512k = BwPair<Bw256k>;

    /// A `Bigwise` type composed of 1.048.576 bits.
    pub type Bw1M = BwPair<Bw512k>;

    impl <T> Bigwise for BwPair<T> where T: Bigwise + Copy {

        fn size() -> u32 {
            2 * T::size()
        }

        fn empty() -> Self {
            BwPair {
                left: T::empty(),
                right: T::empty(),
            }
        }

        fn full() -> Self {
            BwPair {
                left: T::full(),
                right: T::full(),
            }
        }

        fn from_bytes(bytes: &[u8]) -> Self {
            use std::cmp::min;

            let half_bytes = (T::size() / 8) as usize;
            let x1 = bytes.len() - min(bytes.len(), 2 * half_bytes);
            let x2 = bytes.len() - min(bytes.len(), half_bytes);
            BwPair {
                left: if x2 > x1 { T::from_bytes(&bytes[x1..x2]) } else { T::empty() },
                right: T::from_bytes(&bytes[x2..]),
            }
        }

        fn to_bytes(&self) -> Vec<u8> {
            let nb_bytes = (Self::size() / 8) as usize;
            let mut res = Vec::with_capacity(nb_bytes);
            res.extend(&mut self.left.to_bytes().into_iter());
            res.extend(&mut self.right.to_bytes().into_iter());
            res
        }

        fn get(&self, i: u32) -> bool {
            if i >= Self::size() {
                false
            } else if i >= T::size() {
                self.left.get(i - T::size())
            } else {
                self.right.get(i)
            }
        }

        fn set(&mut self, i: u32, v: bool) {
            if i >= Self::size() {
                panic!("index overflowed");
            } else if i >= T::size() {
                self.left.set(i - T::size(), v)
            } else {
                self.right.set(i, v)
            }
        }

        fn iter(&self) -> BitsIter<Self> {
            BitsIter {bw: self, idx: 0}
        }

        fn shift_left(&self, n: u32) -> Self {
            if n == 0 {
                *self
            } else if n < T::size() {
                BwPair {
                    left: self.left.shift_left(n).or(&self.right.shift_right(T::size()-n)),
                    right: self.right.shift_left(n),
                }
            } else if n == T::size() {
                BwPair {
                    left: self.right,
                    right: T::empty(),
                }
            } else if n < Self::size() {
                BwPair {
                    left: self.right.shift_left(n-T::size()),
                    right: T::empty(),
                }
            } else {
                BwPair {
                    left: T::empty(),
                    right: T::empty(),
                }
            }
        }

        fn shift_right(&self, n: u32) -> Self {
            if n == 0 {
                *self
            } else if n < T::size() {
                BwPair {
                    left: self.left.shift_right(n),
                    right: self.right.shift_right(n).or(&self.left.shift_left(T::size()-n)),
                }
            } else if n == T::size() {
                BwPair {
                    left: T::empty(),
                    right: self.left,
                }
            } else if n < Self::size() {
                BwPair {
                    left: T::empty(),
                    right: self.left.shift_right(n-T::size()),
                }
            } else {
                BwPair {
                    left: T::empty(),
                    right: T::empty(),
                }
            }
        }

        fn or(&self, rhs: &Self) -> Self {
            BwPair {
                left: self.left.or(&rhs.left),
                right: self.right.or(&rhs.right),
            }
        }

        fn and(&self, rhs: &Self) -> Self {
            BwPair {
                left: self.left.and(&rhs.left),
                right: self.right.and(&rhs.right),
            }
        }

        fn xor(&self, rhs: &Self) -> Self {
            BwPair {
                left: self.left.xor(&rhs.left),
                right: self.right.xor(&rhs.right),
            }
        }

        fn not(&self) -> Self {
            BwPair {
                left: self.left.not(),
                right: self.right.not(),
            }
        }

        fn rotate_left(&self, n: u32) -> Self {
            let n = n % Self::size();
            if n == 0 {
                *self
            } else if n < T::size() {
                BwPair {
                    left: self.left.shift_left(n).or(&self.right.shift_right(T::size()-n)),
                    right: self.right.shift_left(n).or(&self.left.shift_right(T::size()-n)),
                }
            } else if n == T::size() {
                BwPair {
                    left: self.right,
                    right: self.left,
                }
            } else {
                BwPair {
                    left: self.right.shift_left(n-T::size()).or(&self.left.shift_right(T::size()-n)),
                    right: self.left.shift_right(n-T::size()).or(&self.right.shift_left(T::size()-n)),
                }
            }
        }

        fn rotate_right(&self, n: u32) -> Self {
            let n = n % Self::size();
            if n == 0 {
                *self
            } else if n < T::size() {
                BwPair {
                    left: self.right.shift_right(n).or(&self.left.shift_left(T::size()-n)),
                    right: self.left.shift_right(n).or(&self.right.shift_left(T::size()-n)),
                }
            } else if n == T::size() {
                BwPair {
                    left: self.right,
                    right: self.left,
                }
            } else {
                BwPair {
                    left: self.left.shift_right(n-T::size()).or(&self.right.shift_left(T::size()-n)),
                    right: self.right.shift_left(n-T::size()).or(&self.left.shift_right(T::size()-n)),
                }
            }
        }
    }

    impl <T> Rand for BwPair<T> where T: Bigwise {
        fn rand<R: Rng>(rng: &mut R) -> Self {
            BwPair {
                left: T::rand(rng),
                right: T::rand(rng),
            }
        }
    }

    /// The sequence of zeroes and ones, the MSB (most significant bit)
    /// appearing on the left.
    ///
    impl <T> Debug for BwPair<T> where T: Bigwise {
        fn fmt(&self, f: &mut Formatter) -> Result<(), fmt::Error> {
            try!(self.left.fmt(f));
            try!(self.right.fmt(f));
            Ok(())
        }
    }

    #[cfg(test)]
    mod tests {

        use bigwise::{Bigwise, BwPair, Bw64, Bw128, Bw256};
        use quickcheck::{Arbitrary, Gen, quickcheck, TestResult};

        impl Arbitrary for Bw64 {
            fn arbitrary<G: Gen>(g: &mut G) -> Self {
                g.gen()
            }
        }

        impl <T> Arbitrary for BwPair<T> where T: Bigwise + Send + 'static + Clone {
            fn arbitrary<G: Gen>(g: &mut G) -> Self {
                g.gen()
            }
        }

        fn generic_empty<T>() where T: Bigwise {
            let x = T::empty();
            for i in 0..T::size() {
                assert!(x.get(i) == false);
            }
        }

        #[test] fn bw64_empty() { generic_empty::<Bw64>(); }
        #[test] fn bw128_empty() { generic_empty::<Bw128>(); }
        #[test] fn bw256_empty() { generic_empty::<Bw256>(); }

        fn generic_full<T>() where T: Bigwise {
            let x = T::full();
            for i in 0..T::size() {
                assert!(x.get(i) == true);
            }
        }

        #[test] fn bw64_full() { generic_full::<Bw64>(); }
        #[test] fn bw128_full() { generic_full::<Bw128>(); }
        #[test] fn bw256_full() { generic_full::<Bw256>(); }

        fn generic_set_get<T>() where T: Bigwise {
            for i in 0..T::size() {
                let mut x = T::empty();
                assert!(x.get(i) == false);
                x.set(i, true);
                assert!(x.get(i) == true);
                x.set(i, false);
                assert!(x.get(i) == false);
            }
        }

        #[test] fn bw64_set_get() { generic_set_get::<Bw64>(); }
        #[test] fn bw128_set_get() { generic_set_get::<Bw128>(); }
        #[test] fn bw256_set_get() { generic_set_get::<Bw256>(); }

        fn prop_to_bytes_from_bytes<T>(x: T) -> bool where T: Bigwise {
            x == T::from_bytes(&x.to_bytes())
        }

        #[test] fn bw64_to_bytes_from_bytes() { quickcheck(prop_to_bytes_from_bytes::<Bw64> as fn(Bw64) -> bool); }
        #[test] fn bw128_to_bytes_from_bytes() { quickcheck(prop_to_bytes_from_bytes::<Bw128> as fn(Bw128) -> bool); }
        #[test] fn bw256_to_bytes_from_bytes() { quickcheck(prop_to_bytes_from_bytes::<Bw256> as fn(Bw256) -> bool); }

        fn generic_from_empty_bytes<T>() where T: Bigwise {
            assert!(T::from_bytes(&[]) == T::empty());
        }

        #[test] fn bw64_from_empty_bytes() { generic_from_empty_bytes::<Bw64>(); }
        #[test] fn bw128_from_empty_bytes() { generic_from_empty_bytes::<Bw128>(); }
        #[test] fn bw256_from_empty_bytes() { generic_from_empty_bytes::<Bw256>(); }

        fn generic_from_one_byte<T>() where T: Bigwise {
            let len = (T::size() / 8) as usize;
            let x = T::from_bytes(&[0xFF]).to_bytes();
            assert!(x.len() == len);
            assert!(x.iter().take(len-1).all(|&v| v == 0));
            assert!(x.last() == Some(&0xFF));
        }

        #[test] fn bw64_from_one_byte() { generic_from_one_byte::<Bw64>(); }
        #[test] fn bw128_from_one_byte() { generic_from_one_byte::<Bw128>(); }
        #[test] fn bw256_from_one_byte() { generic_from_one_byte::<Bw256>(); }

        fn generic_iter_empty<T>() where T: Bigwise {
            use std::iter::repeat;
            let v1 : Vec<_> = T::empty().iter().collect();
            let v2 : Vec<_> = repeat(false).take(T::size() as usize).collect();
            assert!(v1 == v2);
            assert!(v1.len() == T::size() as usize);
        }

        #[test] fn bw64_iter_empty() { generic_iter_empty::<Bw64>(); }
        #[test] fn bw128_iter_empty() { generic_iter_empty::<Bw128>(); }
        #[test] fn bw256_iter_empty() { generic_iter_empty::<Bw256>(); }

        fn generic_iter_full<T>() where T: Bigwise {
            use std::iter::repeat;
            let v1 : Vec<_> = T::full().iter().collect();
            let v2 : Vec<_> = repeat(true).take(T::size() as usize).collect();
            assert!(v1 == v2);
            assert!(v1.len() == T::size() as usize);
        }

        #[test] fn bw64_iter_full() { generic_iter_full::<Bw64>(); }
        #[test] fn bw128_iter_full() { generic_iter_full::<Bw128>(); }
        #[test] fn bw256_iter_full() { generic_iter_full::<Bw256>(); }

        fn prop_iter_get<T>(x: T) -> bool where T: Bigwise {
            x.iter().enumerate().all(|(i, b)| x.get(i as u32) == b)
        }

        #[test] fn bw64_iter_get() { quickcheck(prop_iter_get::<Bw64> as fn(Bw64) -> bool); }
        #[test] fn bw128_iter_get() { quickcheck(prop_iter_get::<Bw128> as fn(Bw128) -> bool); }
        #[test] fn bw256_iter_get() { quickcheck(prop_iter_get::<Bw256> as fn(Bw256) -> bool); }

        fn prop_shift_left<T>((original, n) : (T, u32)) -> TestResult where T: Bigwise {
            if n >= T::size() {
                return TestResult::discard();
            }
            let shifted = original.shift_left(n);
            let v_original : Vec<_> = original.iter().take((T::size()-n) as usize).collect();
            let v_shifted : Vec<_> = shifted.iter().skip(n as usize).collect();
            TestResult::from_bool(v_original == v_shifted)
        }

        #[test] fn bw64_shift_left() { quickcheck(prop_shift_left::<Bw64> as fn((Bw64, u32)) -> TestResult); }
        #[test] fn bw128_shift_left() { quickcheck(prop_shift_left::<Bw128> as fn((Bw128, u32)) -> TestResult); }
        #[test] fn bw256_shift_left() { quickcheck(prop_shift_left::<Bw256> as fn((Bw256, u32)) -> TestResult); }

        fn prop_shift_right<T>((original, n) : (T, u32)) -> TestResult where T: Bigwise {
            if n >= T::size() {
                return TestResult::discard();
            }
            let shifted = original.shift_right(n);
            let v_original : Vec<_> = original.iter().skip(n as usize).collect();
            let v_shifted : Vec<_> = shifted.iter().take((T::size()-n) as usize).collect();
            TestResult::from_bool(v_original == v_shifted)
        }

        #[test] fn bw64_shift_right() { quickcheck(prop_shift_right::<Bw64> as fn((Bw64, u32)) -> TestResult); }
        #[test] fn bw128_shift_right() { quickcheck(prop_shift_right::<Bw128> as fn((Bw128, u32)) -> TestResult); }
        #[test] fn bw256_shift_right() { quickcheck(prop_shift_right::<Bw256> as fn((Bw256, u32)) -> TestResult); }

        fn prop_or<T>(xx: (T, T)) -> bool where T: Bigwise {
            let res = xx.0.or(&xx.1);
            xx.0.iter().zip(xx.1.iter())
                .map(|(x1,x2)| x1 | x2)
                .zip(res.iter())
                .all(|(expected, observed)| expected == observed)
        }

        #[test] fn bw64_or() { quickcheck(prop_or::<Bw64> as fn((Bw64, Bw64)) -> bool); }
        #[test] fn bw128_or() { quickcheck(prop_or::<Bw128> as fn((Bw128, Bw128)) -> bool); }
        #[test] fn bw256_or() { quickcheck(prop_or::<Bw256> as fn((Bw256, Bw256)) -> bool); }
    }
}