1 #include "tommath_private.h"
3 /* LibTomMath, multiple-precision integer library -- Tom St Denis */
4 /* SPDX-License-Identifier: Unlicense */
6 /* two complement and */
7 mp_err
mp_and(const mp_int
*a
, const mp_int
*b
, mp_int
*c
)
9 int used
= MP_MAX(a
->used
, b
->used
) + 1, i
;
11 mp_digit ac
= 1, bc
= 1, cc
= 1;
12 bool neg
= (mp_isneg(a
) && mp_isneg(b
));
14 if ((err
= mp_grow(c
, used
)) != MP_OKAY
) {
18 for (i
= 0; i
< used
; i
++) {
21 /* convert to two complement if negative */
23 ac
+= (i
>= a
->used
) ? MP_MASK
: (~a
->dp
[i
] & MP_MASK
);
27 x
= (i
>= a
->used
) ? 0uL : a
->dp
[i
];
30 /* convert to two complement if negative */
32 bc
+= (i
>= b
->used
) ? MP_MASK
: (~b
->dp
[i
] & MP_MASK
);
36 y
= (i
>= b
->used
) ? 0uL : b
->dp
[i
];
41 /* convert to to sign-magnitude if negative */
43 cc
+= ~c
->dp
[i
] & MP_MASK
;
44 c
->dp
[i
] = cc
& MP_MASK
;
50 c
->sign
= (neg
? MP_NEG
: MP_ZPOS
);