PageRenderTime 29ms CodeModel.GetById 24ms app.highlight 4ms RepoModel.GetById 0ms app.codeStats 0ms

/cln-1.3.2/src/integer/bitwise/cl_I_ldb.cc

#
C++ | 52 lines | 26 code | 8 blank | 18 comment | 4 complexity | 656dfca97644f772e684e4ee1391cd20 MD5 | raw file
Possible License(s): GPL-2.0
 1// ldb().
 2
 3// General includes.
 4#include "base/cl_sysdep.h"
 5
 6// Specification.
 7#include "cln/integer.h"
 8
 9
10// Implementation.
11
12#include "cln/integer.h"
13#include "integer/cl_I.h"
14#include "integer/bitwise/cl_I_byte.h"
15
16namespace cln {
17
18const cl_I ldb (const cl_I& n, const cl_byte& b)
19{
20      // Methode:
21      // (ldb (byte s p) n) extrahiere die Bits p,...,p+s-1 von n.
22      // l:=(integer-length n)
23      // Falls l <= p :
24      //   Falls n>=0: 0, falls n<0: 2^s - 1 (s Einsenbits).
25      // Falls p <= l :
26      //   q:=min(p+s,l).
27      //   Extrahiere die Bits p,...,q-1 von n.
28      //   Falls p+s>l und n<0, f?ge p+s-l Einsenbits an (addiere 2^s-2^(l-p)).
29      var uintC s = b.size;
30      var uintC p = b.position;
31      var uintC l = integer_length(n); // l = (integer-length n)
32      if (l<=p)
33        // l<=p
34        if (!minusp(n))
35          // n>=0
36          return 0; // 0 als Ergebnis
37          else
38          // n<0
39          return cl_fullbyte(0,s); // 2^s-2^0 als Ergebnis
40        else
41        // l>p
42        { var cl_I erg = ldb_extract(n,p,(p+s<l ? p+s : l));
43          var uintC lp = l-p;
44          if ((s>lp) && minusp(n)) // s>l-p und n<0 ?
45            { return logior(erg,cl_fullbyte(lp,s)); } // setze Bits l-p,...,s-1
46            // (logisches Exklusiv-Oder oder Addition ginge auch)
47            else
48            return erg;
49        }
50}
51
52}  // namespace cln