1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
void bigint_neg(bigint *dest) { for (u4 i=0;i<dest->len;++i) { dest->dats[i]=~dest->dats[i]; } add_u4(dest->len, dest->dats,1); } u4 bigint_createnegcopy(bigint *xi, const bigint *src) { xi->len=src->len; xi->dats=(u4*) malloc (xi->len* sizeof (u4)); if (xi->dats==NULL) return 0; for ( size_t i=0;i<xi->len;i++) { xi->dats[i]=~src->dats[i]; } add_u4(xi->len, xi->dats, 1); return xi->len; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
inline u4 sub_a(u4 len,u4*dest,u4*src) { u4 carry=1; u4 temp; for (u4 i=0;i<len;++i) { temp=~src[i]; dest[i]+=temp; if (carry) { if (dest[i]>=temp)carry=0; if (dest[i]==0xFFFFFFFF)carry=1; ++dest[i]; } else { if (dest[i]<temp)carry=1; } } return carry; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
inline u4 sub_u4(u4 len, u4*dest, u4 src) { u4 carry=0; dest[0]-=src; if (dest[0]>=src)carry=1; for (u4 i=1;i<len;++i) { if (carry) { if (dest[i]!=0)carry=0; --dest[i]; } else { break ; } } return carry; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
void bigint_sub(bigint *dest, const bigint *src) { if (dest->len<src->len) { sub_a(dest->len,dest->dats,src->dats); } else { if (sub_a(src->len,dest->dats,src->dats)) { sub_u4(dest->len-src->len,&dest->dats[src->len],1); } } } |
BigInteger를 구현해보자 - 7. 나누기 (2) | 2009.04.25 |
---|---|
BigInteger를 구현해보자 - 6. 크기비교 (4) | 2009.04.21 |
BigInteger를 구현해보자 - 5. 시프트 연산 (0) | 2009.04.12 |
BigInteger를 구현해보자 - 4. 곱셈과 부호 체크 (0) | 2009.03.29 |
BigInteger를 구현해보자 - 2. 정의와 생성, 파괴, 덧셈 (8) | 2009.03.29 |
BigInteger를 구현해보자 - 1. BigInteger의 개요 (2) | 2009.03.29 |
댓글 영역