12 from __future__
import print_function
15 VERSION =
'bit32 --- 2010 March 16'
25 BIN_8_TUPLE = (
'0',
'1',
'10',
'11',
26 '100',
'101',
'110',
'111',
27 '1000',
'1001',
'1010',
'1011',
28 '1100',
'1101',
'1110',
'1111',
29 '10000',
'10001',
'10010',
'10011',
30 '10100',
'10101',
'10110',
'10111',
31 '11000',
'11001',
'11010',
'11011',
32 '11100',
'11101',
'11110',
'11111',
33 '100000',
'100001',
'100010',
'100011',
34 '100100',
'100101',
'100110',
'100111',
35 '101000',
'101001',
'101010',
'101011',
36 '101100',
'101101',
'101110',
'101111',
37 '110000',
'110001',
'110010',
'110011',
38 '110100',
'110101',
'110110',
'110111',
39 '111000',
'111001',
'111010',
'111011',
40 '111100',
'111101',
'111110',
'111111',
41 '1000000',
'1000001',
'1000010',
'1000011',
42 '1000100',
'1000101',
'1000110',
'1000111',
43 '1001000',
'1001001',
'1001010',
'1001011',
44 '1001100',
'1001101',
'1001110',
'1001111',
45 '1010000',
'1010001',
'1010010',
'1010011',
46 '1010100',
'1010101',
'1010110',
'1010111',
47 '1011000',
'1011001',
'1011010',
'1011011',
48 '1011100',
'1011101',
'1011110',
'1011111',
49 '1100000',
'1100001',
'1100010',
'1100011',
50 '1100100',
'1100101',
'1100110',
'1100111',
51 '1101000',
'1101001',
'1101010',
'1101011',
52 '1101100',
'1101101',
'1101110',
'1101111',
53 '1110000',
'1110001',
'1110010',
'1110011',
54 '1110100',
'1110101',
'1110110',
'1110111',
55 '1111000',
'1111001',
'1111010',
'1111011',
56 '1111100',
'1111101',
'1111110',
'1111111',
57 '10000000',
'10000001',
'10000010',
'10000011',
58 '10000100',
'10000101',
'10000110',
'10000111',
59 '10001000',
'10001001',
'10001010',
'10001011',
60 '10001100',
'10001101',
'10001110',
'10001111',
61 '10010000',
'10010001',
'10010010',
'10010011',
62 '10010100',
'10010101',
'10010110',
'10010111',
63 '10011000',
'10011001',
'10011010',
'10011011',
64 '10011100',
'10011101',
'10011110',
'10011111',
65 '10100000',
'10100001',
'10100010',
'10100011',
66 '10100100',
'10100101',
'10100110',
'10100111',
67 '10101000',
'10101001',
'10101010',
'10101011',
68 '10101100',
'10101101',
'10101110',
'10101111',
69 '10110000',
'10110001',
'10110010',
'10110011',
70 '10110100',
'10110101',
'10110110',
'10110111',
71 '10111000',
'10111001',
'10111010',
'10111011',
72 '10111100',
'10111101',
'10111110',
'10111111',
73 '11000000',
'11000001',
'11000010',
'11000011',
74 '11000100',
'11000101',
'11000110',
'11000111',
75 '11001000',
'11001001',
'11001010',
'11001011',
76 '11001100',
'11001101',
'11001110',
'11001111',
77 '11010000',
'11010001',
'11010010',
'11010011',
78 '11010100',
'11010101',
'11010110',
'11010111',
79 '11011000',
'11011001',
'11011010',
'11011011',
80 '11011100',
'11011101',
'11011110',
'11011111',
81 '11100000',
'11100001',
'11100010',
'11100011',
82 '11100100',
'11100101',
'11100110',
'11100111',
83 '11101000',
'11101001',
'11101010',
'11101011',
84 '11101100',
'11101101',
'11101110',
'11101111',
85 '11110000',
'11110001',
'11110010',
'11110011',
86 '11110100',
'11110101',
'11110110',
'11110111',
87 '11111000',
'11111001',
'11111010',
'11111011',
88 '11111100',
'11111101',
'11111110',
'11111111')
98 MERSENNE32 = 4294967295
102 MERSENNE_TUPLE = (0, 1, 3, 7,
104 MERSENNE8, 511, 1023, 2047,
105 4095, 8191, 16383, 32767,
106 MERSENNE16, 131071, 262143, 524287,
107 1048575, 2097151, 4194303, 8388607,
108 MERSENNE24, 33554431, 67108863, 134217727,
109 268435455, 536870911, 1073741823, 2147483647,
114 NBBITS1_8_TUPLE = (0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4,
115 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
116 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
117 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
118 1, 2, 2,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 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
142 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
143 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
144 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
145 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
146 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
147 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
148 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8)
152 POW2_TUPLE = (1, 2, 4, 8,
154 256, 512, 1024, 2048,
155 4096, 8192, 16384, 32768,
156 65536, 131072, 262144, 524288,
157 1048576, 2097152, 4194304, 8388608,
158 16777216, 33554432, 67108864, 134217728,
159 268435456, 536870912, 1073741824, 2147483648)
163 RSCAN0_8_TUPLE = (
None,
None, 0,
None, 1, 1, 0,
None, 2, 2, 2, 2, 1, 1, 0,
None,
164 3, 3, 3, 3, 3, 3, 3, 3, 2, 2, 2, 2, 1, 1, 0,
None,
165 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
166 3, 3, 3, 3, 3, 3, 3, 3, 2, 2, 2, 2, 1, 1, 0,
None,
167 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
168 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
169 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
170 3, 3, 3, 3, 3, 3, 3, 3, 2, 2, 2, 2, 1, 1, 0,
None,
171 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
172 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,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1, 2, 1, 4, 1, 2, 1, 3, 1, 2, 1,
199 6, 1, 2, 1, 3, 1, 2, 1, 4, 1, 2, 1, 3, 1, 2, 1,
200 5, 1, 2, 1, 3, 1, 2, 1, 4, 1, 2, 1, 3, 1, 2, 1)
209 """Renvoie un string représentant n en binaire
210 (Si n == 0 alors renvoie '0')
212 Pre: n naturel < 2**32
214 Result: string de 1 <= longueur <= 32
220 return BIN_8_TUPLE[n]
222 l = [BIN_8_TUPLE[n & MERSENNE8]]
226 l.insert(0,
'0' * (8 - len(l[0])))
227 l.insert(0, BIN_8_TUPLE[n & MERSENNE8])
233 """Renvoie True si le bit d'indice i de n vaut 1,
236 Pre: n naturel < 2**32
246 return bool(n & (1 << i))
251 """Si b alors renvoie n avec son bit d'indice i à 1,
252 sinon renvoie n avec son bit d'indice i à 0
254 Pre: n naturel < 2**32
257 Result: naturel < 2**32
264 return (n | (1 << i)
if b
270 """Renvoie n avec son bit d'indice i à 0
272 Pre: n naturel < 2**32
275 Result: naturel < 2**32
287 """Renvoie n avec son bit d'indice i à 1
289 Pre: n naturel < 2**32
292 Result: naturel < 2**32
305 """Renvoie lg(n) == log_2(n) == nbbits(n) - 1
306 == l'indice du dernier bit à 1 de n
308 Pre: n: 1 <= naturel < 2**32
320 """Renvoie le nième nombre de Mersenne M_k == 2**k - 1
322 Pre: k: naturel <= 32
324 Result: naturel < 2**32
330 return MERSENNE_TUPLE[k]
335 """Renvoie True si n est un nombre de Mersenne,
338 Pre: n: naturel < 2**32
345 return MERSENNE_TUPLE[
scan0(n)] == n
350 """Si n == M_k alors renvoie k
353 Pre: n: naturel < 2**32
355 Result: naturel <= 32 ou None
361 return (i
if MERSENNE_TUPLE[i] == n
367 """Renvoie le nombre de bits à 0 de n
368 (sans tenir compte des 0 après le dernier 1)
369 (Si n == 0 alors renvoie 0)
371 Pre: n: naturel < 2**32
373 Result: naturel <= 31
383 """Renvoie le nombre de bits à 1 de n
384 (Si n == 0 alors renvoie 0)
386 Pre: n: naturel < 2**32
388 Result: naturel <= 32
393 return (NBBITS1_8_TUPLE[n >> 24] + NBBITS1_8_TUPLE[(n >> 16) & MERSENNE8]
394 + NBBITS1_8_TUPLE[(n >> 8) & MERSENNE8] + NBBITS1_8_TUPLE[n & MERSENNE8])
399 """Renvoie le nombre de bits de n
400 == nbbits0(n) + nbbits1(n)
401 (sans tenir compte des 0 après le dernier 1)
402 (Si n == 0 alors renvoie 0)
404 Pre: n: naturel < 2**32
406 Result: naturel <= 32
414 return NBBITS_8_TUPLE[h >> 8] + 24
416 return NBBITS_8_TUPLE[h & MERSENNE8] + 16
419 return NBBITS_8_TUPLE[n>>8 & MERSENNE8] + 8
421 return NBBITS_8_TUPLE[n & MERSENNE8]
426 """Renvoie la kième puissance de 2 : 2**k
430 Result: 1 <= naturel <= 2**31
441 """Renvoie True si n est une puissance de 2,
444 Pre: n: naturel < 2**32
451 return (n != 0)
and (POW2_TUPLE[
rscan1(n)] == n)
456 """Si n == 2^k alors renvoie k
459 Pre: n: naturel < 2**32
461 Result: naturel <= 32 ou None
467 return (i
if (n != 0)
and (POW2_TUPLE[i] == n)
473 """Renvoie l'indice du dernier bit à 0 de n
474 ou None si pas de bit à 0 contenu dans n
476 Pre: n: naturel < 2**32
478 Result: naturel <= 32
484 if (h&MERSENNE16 != 0)
and (h != MERSENNE16):
485 if (h>>8 != 0)
and (h>>8 != MERSENNE8):
486 return RSCAN0_8_TUPLE[h >> 8] + 24
488 return RSCAN0_8_TUPLE[h & MERSENNE8] + 16
491 if (n>>8 != 0)
and (h>>8 != MERSENNE8):
492 return RSCAN0_8_TUPLE[n >> 8] + 8
494 return RSCAN0_8_TUPLE[n & MERSENNE8]
499 """Renvoie l'indice du dernier bit à 1 de n
502 Pre: n: naturel < 2**32
504 Result: naturel <= 31 ou None
509 return (
nbbits(n) - 1
if n > 0
515 """Renvoie l'indice du premier bit à 0 de n
516 (si n == 0 alors renvoie 0)
517 (si n == 2**32 - 1 alors renvoie 32)
519 Pre: n: naturel < 2**32
521 Result: naturel <= 32
527 if (n & MERSENNE16) != MERSENNE16:
528 if (n & MERSENNE8) != MERSENNE8:
534 if (n & (MERSENNE8 << 16)) != (MERSENNE8 << 16):
540 return (k
if n&1 == 0
541 else k + SCAN0_ODD8_TUPLE[(n & MERSENNE8) >> 1])
548 """Renvoie l'indice du premier bit à 1 de n
551 Pre: n: naturel < 2**32
553 Result: naturel < 32 ou None
559 if (n & MERSENNE16) != 0:
560 if (n & MERSENNE8) != 0:
566 if (n & (MERSENNE8 << 16)) != 0:
572 return (k
if n&1 != 0
573 else k + SCAN1_EVEN8_TUPLE[(n & MERSENNE8) >> 1])
582 if __name__ ==
'__main__':
590 if not 'profile' in dir():
596 debug.test_begin(VERSION, __debug__)
598 assert len(BIN_8_TUPLE) == 256, len(BIN_8_TUPLE)
599 assert len(MERSENNE_TUPLE) == 33, len(MERSENNE_TUPLE)
600 assert len(NBBITS1_8_TUPLE) == 256, len(NBBITS1_8_TUPLE)
601 assert len(NBBITS_8_TUPLE) == 256, len(NBBITS_8_TUPLE)
602 assert len(POW2_TUPLE) == 32, len(POW2_TUPLE)
603 assert len(RSCAN0_8_TUPLE) == 256, len(RSCAN0_8_TUPLE)
604 assert len(SCAN0_ODD8_TUPLE) == 128, len(SCAN0_ODD8_TUPLE)
605 assert len(SCAN1_EVEN8_TUPLE) == 128, len(SCAN1_EVEN8_TUPLE)
608 print(
'bin()...', end=
''); sys.stdout.flush()
609 assert bin(0) ==
'0',
bin(0)
610 assert bin(1) ==
'1',
bin(1)
611 for i
in range(1, 31):
612 assert bin(2**i - 1) ==
'1'*i, (i,
bin(2**i - 1))
613 assert bin(2**i) ==
'1' +
'0'*i, (i,
bin(2**i))
614 assert bin(2**i + 1) ==
'1' +
'0'*(i - 1) +
'1', (i,
bin(2**i + 1))
615 assert bin(2**32 - 1) ==
'1'*32,
bin(2**32)
616 if debug.assertspeed >= debug.ASSERT_NORMAL:
617 for n
in range(1, 16384):
618 assert int(
bin(n), 2) == n, (n,
bin(n))
620 for n
in range(1, 2**32, 10000000):
621 assert int(
bin(n), 2) == n, (n,
bin(n))
623 for n
in range(2**32 - 16384, 2**32):
624 assert int(
bin(n), 2) == n, (n,
bin(n))
627 print(debug.assertspeed_str(), end=
'')
628 print(
'ok'); sys.stdout.flush()
631 print(
'bit()...', end=
''); sys.stdout.flush()
633 assert bit(2**i, i) ==
True, (i, 2**i,
bit(2**i, i))
635 assert bit(0, i) ==
False, (i,
bit(0, i))
636 assert bit(MERSENNE32, i) ==
True, (i,
bit(MERSENNE32, i))
638 assert bit(2**i, j) ==
False, (i, j, 2**i,
bit(2**i, j))
639 print(
'ok'); sys.stdout.flush()
642 print(
'bitset()...', end=
''); sys.stdout.flush()
643 for n
in range(2048):
646 assert bitset(n, i,
False) == n - (1<<i), (n, n - (1<<i),
bitset(n, i,
False))
649 assert bitset(n, i,
False) == n, (n,
bitset(n, i,
False))
650 assert bitset(n, i,
True) == n + (1<<i), (n, n + (1<<i),
bitset(n, i,
True))
651 if debug.assertspeed >= debug.ASSERT_NORMAL:
652 for n
in range(1, 2**32, 10000000):
655 assert bitset(n, i,
False) == n - (1<<i), (n, n - (1<<i),
659 assert bitset(n, i,
False) == n, (n,
bitset(n, i,
False))
660 assert bitset(n, i,
True) == n + (1<<i), (n, n + (1<<i),
bitset(n, i,
True))
661 for n
in range(2**32 - 2048, 2**32):
664 assert bitset(n, i,
False) == n - (1<<i), (n, n - (1<<i),
668 assert bitset(n, i,
False) == n, (n,
bitset(n, i,
False))
669 assert bitset(n, i,
True) == n + (1<<i), (n, n + (1<<i),
bitset(n, i,
True))
671 print(debug.assertspeed_str(), end=
'')
672 print(
'ok'); sys.stdout.flush()
675 print(
'bitset0()...', end=
''); sys.stdout.flush()
676 for n
in range(2048):
679 assert bitset0(n, i) == n - (1<<i), (n, n - (1<<i),
bitset0(n, i))
683 if debug.assertspeed >= debug.ASSERT_NORMAL:
684 for n
in range(1, 2**32, 10000000):
687 assert bitset0(n, i) == n - (1<<i), (n, n - (1<<i),
bitset0(n, i))
691 for n
in range(2**32 - 2048, 2**32):
694 assert bitset0(n, i) == n - (1<<i), (n, n - (1<<i),
bitset0(n, i))
699 print(debug.assertspeed_str(), end=
'')
700 print(
'ok'); sys.stdout.flush()
703 print(
'bitset1()...', end=
''); sys.stdout.flush()
704 for n
in range(2048):
709 assert bitset1(n, i) == n + (1<<i), (n, n + (1<<i),
bitset1(n, i))
711 if debug.assertspeed >= debug.ASSERT_NORMAL:
712 for n
in range(1, 2**32, 10000000):
717 assert bitset1(n, i) == n + (1<<i), (n, n + (1<<i),
bitset1(n, i))
719 for n
in range(2**32 - 2048, 2**32):
724 assert bitset1(n, i) == n + (1<<i), (n, n + (1<<i),
bitset1(n, i))
727 print(debug.assertspeed_str(), end=
'')
728 print(
'ok'); sys.stdout.flush()
731 print(
'lg()...', end=
''); sys.stdout.flush()
732 for n
in range(1, 4096):
733 assert lg(n) == len(
bin(n)) - 1, (n,
lg(n), len(
bin(n)))
734 if debug.assertspeed >= debug.ASSERT_NORMAL:
735 for n
in range(1, 2**32, 10000000):
736 assert lg(n) == len(
bin(n)) - 1, (n,
lg(n), len(
bin(n)))
737 for n
in range(2**32 - 4096, 2**32):
738 assert lg(n) == len(
bin(n)) - 1, (n,
lg(n), len(
bin(n)))
740 print(debug.assertspeed_str(), end=
'')
741 print(
'ok'); sys.stdout.flush()
744 print(
'mersenne()...', end=
''); sys.stdout.flush()
747 print(
'ok'); sys.stdout.flush()
750 print(
'mersenne_is()...', end=
''); sys.stdout.flush()
755 for k
in range(2, 32):
759 print(
'ok'); sys.stdout.flush()
762 print(
'mersenne_to_index()...', end=
''); sys.stdout.flush()
767 for k
in range(2, 32):
774 print(
'ok'); sys.stdout.flush()
777 print(
'nbbits0()...', end=
''); sys.stdout.flush()
779 for n
in range(1, 4096):
781 for i
in range(
rscan1(n)):
785 if debug.assertspeed >= debug.ASSERT_NORMAL:
786 for n
in range(1, 2**32, 10000000):
788 for i
in range(
rscan1(n)):
792 for n
in range(2**32 - 4096, 2**32):
794 for i
in range(
rscan1(n)):
799 print(debug.assertspeed_str(), end=
'')
800 print(
'ok'); sys.stdout.flush()
803 print(
'nbbits1()...', end=
''); sys.stdout.flush()
805 for n
in range(4096):
811 if debug.assertspeed >= debug.ASSERT_NORMAL:
812 for n
in range(1, 2**32, 10000000):
818 for n
in range(2**32 - 4096, 2**32):
825 print(debug.assertspeed_str(), end=
'')
826 print(
'ok'); sys.stdout.flush()
829 print(
'nbbits()...', end=
''); sys.stdout.flush()
835 for n
in range(1, 4096):
837 assert (1<<(l - 1)) <= n, (n, l)
838 assert n < (1<<l), (n, l)
840 if debug.assertspeed >= debug.ASSERT_NORMAL:
841 for n
in range(1, 2**32, 10000000):
843 assert (1<<(l - 1)) <= n, (n, l)
844 assert n < (1<<l), (n, l)
846 for n
in range(2**32-4096, 2**32):
848 assert (1<<(l - 1)) <= n, (n, l)
849 assert n < (1<<l), (n, l)
852 print(debug.assertspeed_str(), end=
'')
853 print(
'ok'); sys.stdout.flush()
856 print(
'pow2()...', end=
''); sys.stdout.flush()
858 assert pow2(k) == 2**k, (k,
pow2(k))
859 print(
'ok'); sys.stdout.flush()
862 print(
'pow2_is()...', end=
''); sys.stdout.flush()
869 for k
in range(2, 32):
873 print(
'ok'); sys.stdout.flush()
876 print(
'pow2_to_index()...', end=
''); sys.stdout.flush()
883 for k
in range(2, 32):
890 print(
'ok'); sys.stdout.flush()
893 print(
'rscan0()...', end=
''); sys.stdout.flush()
900 assert rscan0(MERSENNE32) ==
None
901 for n
in range(4096):
903 for i
in range(
nbbits(n) - 1, -1, 1):
908 if debug.assertspeed >= debug.ASSERT_NORMAL:
909 for n
in range(1, 2**32, 10000000):
911 for i
in range(
nbbits(n) - 1, -1, 1):
916 for n
in range(2**32 - 4096, 2**32):
918 for i
in range(
nbbits(n) - 1, -1, 1):
924 print(debug.assertspeed_str(), end=
'')
925 print(
'ok'); sys.stdout.flush()
928 print(
'rscan1()...', end=
''); sys.stdout.flush()
935 assert rscan1(MERSENNE32) == 31
936 for n
in range(1, 4096):
938 if debug.assertspeed >= debug.ASSERT_NORMAL:
939 for n
in range(1, 2**32, 10000000):
941 for n
in range(2**32 - 4096, 2**32):
944 print(debug.assertspeed_str(), end=
'')
945 print(
'ok'); sys.stdout.flush()
948 print(
'scan0()...', end=
''); sys.stdout.flush()
950 assert scan0(2**32 - 1) == 32,
scan0(2**32 - 1)
952 assert scan0(2**i - 1) == i, (i,
scan0(2**i - 1))
953 for n
in range(32768):
958 if debug.assertspeed >= debug.ASSERT_NORMAL:
959 for n
in range(1, 2**32, 1000000):
964 for n
in range(2**32 - 32768, 2**32):
970 print(debug.assertspeed_str(), end=
'')
971 print(
'ok'); sys.stdout.flush()
974 print(
'scan1()...', end=
''); sys.stdout.flush()
978 for n
in range(1, 32768):
983 if debug.assertspeed >= debug.ASSERT_NORMAL:
984 for n
in range(1, 2**32, 1000000):
989 for n
in range(2**32 - 32768, 2**32):
995 print(debug.assertspeed_str(), end=
'')
996 print(
'ok'); sys.stdout.flush()