1
|
1 /* LibTomMath, multiple-precision integer library -- Tom St Denis |
|
2 * |
|
3 * LibTomMath is a library that provides multiple-precision |
|
4 * integer arithmetic as well as number theoretic functionality. |
|
5 * |
|
6 * The library was designed directly after the MPI library by |
|
7 * Michael Fromberger but has been written from scratch with |
|
8 * additional optimizations in place. |
|
9 * |
|
10 * The library is free for all purposes without any express |
|
11 * guarantee it works. |
|
12 * |
|
13 * Tom St Denis, [email protected], http://math.libtomcrypt.org |
|
14 */ |
|
15 #include <tommath.h> |
|
16 |
|
17 /* returns the number of bits in an int */ |
|
18 int |
|
19 mp_count_bits (mp_int * a) |
|
20 { |
|
21 int r; |
|
22 mp_digit q; |
|
23 |
|
24 /* shortcut */ |
|
25 if (a->used == 0) { |
|
26 return 0; |
|
27 } |
|
28 |
|
29 /* get number of digits and add that */ |
|
30 r = (a->used - 1) * DIGIT_BIT; |
|
31 |
|
32 /* take the last digit and count the bits in it */ |
|
33 q = a->dp[a->used - 1]; |
|
34 while (q > ((mp_digit) 0)) { |
|
35 ++r; |
|
36 q >>= ((mp_digit) 1); |
|
37 } |
|
38 return r; |
|
39 } |