1 // =============================================================================
2 // Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 // Copyright (C) 2008 - INRIA - Pierre MARECHAL <pierre.marechal@inria.fr>
4 // Copyright (C) 2017 - Samuel GOUGEON
6 // This file is distributed under the same license as the Scilab package.
7 // =============================================================================
9 // <-- CLI SHELL MODE -->
10 // <-- NO CHECK REF -->
13 // =============================================================================
15 A = floor(2^52 * rand(20,20));
17 B = strcat(string(bitget(A(i), 1:52)));
18 C = strrev(dec2bin(A(i),52));
19 assert_checkequal(B, C);
23 for i = [1 2 4 8 11 12 14 18] // Loop on int types
24 m = 8 * modulo(i,10) - 1 + 1*(i>10); // Maximal bit rank
25 m = min(m, 52); // due to dec2base() limitation to 2^52
26 A = iconvert(floor( 2^m * rand(20,20)), i);
27 for k = 1:size(A,"*");
28 B = strcat(string(bitget(A(k), 1:m)));
29 C = strrev(dec2bin(A(k), m));
30 assert_checkequal(B, C);
35 // =============================================================================
36 A0 = bin2dec(["0001" "0010";"0011" "0100"]);
39 for i = [0 1 2 4 8 11 12 14 18]
41 assert_checktrue(and( bitget(A,1)==B));
42 assert_checktrue(and( bitget(A,2)==C));
45 // Test 3: about bitmax
46 // =============================================================================
47 k = [0 1 2 4 8 11 12 14 18];
48 bitmax = [1024 7 15 31 63 8 16 32 64];
50 n = iconvert(123, k(i));
51 assert_checkequal(execstr("bitget(n, bitmax(i));","errcatch"), 0);
52 assert_checkfalse(execstr("bitget(n, bitmax(i)+1);" ,"errcatch")==0);
53 assert_checkfalse(execstr("bitget(n, 0);" ,"errcatch")==0);
56 // Tests about input / output types
57 // =============================================================================
58 it = [0 1 2 4 8 11 12 14 18];
60 n = abs(iconvert([1 10 100 271 1000 3467 34567], i));
62 r = bitget(n, iconvert([1:7], j));
63 assert_checkequal(inttype(r), i);
67 // Tests about input / output sizes
68 // =============================================================================
72 // x scalar, pos matrix:
73 r = bitget(n, [1 2 3 ; 5 6 7]);
74 assert_checkequal(size(r), [2 3]);
75 assert_checkequal(r, [1 0 1 ; 0 1 1]);
77 // x matrix, pos scalar:
78 r = bitget(n*[1 2 4 ; 8 16 32]', 6);
79 assert_checkequal(size(r), [3 2]);
80 assert_checkequal(r, [1 0 1 ; 1 0 1]');
82 // x and pos matrices of identical sizes
85 pos = [1 3 5 7 9 ; 8 6 4 2 1];
87 assert_checkequal(size(r), [2 5]);
88 assert_checkequal(r, [1 1 0 0 0 ; 0 0 1 0 1]);
90 // x and pos are arrays with mismatching sizes
91 x = [39 8 4 44 52 5 6 14 64 39 12 4 62 29 12 50 39 29];
111 assert_checkequal(bitget(x, [5 8 12 39]), ref);
113 // Bits extraction from decimal numbers > 2^52
114 // =============================================================================
115 assert_checkequal(execstr("bitget(123 , 54);","errcatch"), 0);
116 assert_checkequal(execstr("bitget(123 , 1000);","errcatch"), 0);
118 // The extracted values of lower bits below %eps must all be %nan:
119 assert_checktrue(and(isnan(bitget(2^70 , 1:17))));
120 assert_checkequal(bitget(1+2.^[51 53 ; 54 5], [2 1 ; 2 1]), [0 %nan ; %nan 1]);
122 // We build a random integer with known bits #0-99
123 // Beware of the bug http://bugzilla.scilab.org/15276
124 i = matrix(1:100, 10, 10);
125 bv = grand(10, 10, "uin", 0, 1) // Bits values
126 bp = bv.*(i-1); // Related powers
128 // We now extract all its bits with bitget()
130 // We select extracted bits that have a known value
132 // We built the related number. Lower bits are ignored:
133 va = sum(2 .^(bg(s).*(i(s)-1)));
134 // Is it equal to v? Yes: bitget() works like a charm on huge decimal numbers!
135 assert_checkequal(va, v);