1 /* mpz_clrbit -- clear a specified bit.
3 Copyright (C) 1991 Free Software Foundation, Inc.
5 This file is part of the GNU MP Library.
7 The GNU MP Library is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 The GNU MP Library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with the GNU MP Library; see the file COPYING. If not, write to
19 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
24 #define MPN_NORMALIZE(p, size) \
27 for (i = (size) - 1; i >= 0; i--) \
35 mpz_clrbit (MP_INT *d, unsigned long int bit_index)
37 mpz_clrbit (d, bit_index)
39 unsigned long int bit_index;
42 mp_size dsize = d->size;
46 limb_index = bit_index / BITS_PER_MP_LIMB;
49 if (limb_index < dsize)
51 dp[limb_index] &= ~((mp_limb) 1 << (bit_index % BITS_PER_MP_LIMB));
52 MPN_NORMALIZE (dp, dsize);
62 /* Simulate two's complement arithmetic, i.e. simulate
63 1. Set OP = ~(OP - 1) [with infinitely many leading ones].
65 3. Set OP = ~OP + 1. */
69 /* No upper bound on this loop, we're sure there's a non-zero limb
71 for (zero_bound = 0; ; zero_bound++)
72 if (dp[zero_bound] != 0)
75 if (limb_index > zero_bound)
77 if (limb_index < dsize)
79 dp[limb_index] |= ((mp_limb) 1 << (bit_index % BITS_PER_MP_LIMB));
83 /* Ugh. The bit should be cleared outside of the end of the
84 number. We have to increase the size of the number. */
85 if (d->alloc < limb_index + 1)
87 _mpz_realloc (d, limb_index + 1);
90 MPN_ZERO (dp + dsize, limb_index - dsize);
91 dp[limb_index] = ((mp_limb) 1 << (bit_index % BITS_PER_MP_LIMB));
92 d->size = -(limb_index + 1);
95 else if (limb_index == zero_bound)
97 dp[limb_index] = ((dp[limb_index] - 1)
98 | ((mp_limb) 1 << (bit_index % BITS_PER_MP_LIMB))) + 1;
99 if (dp[limb_index] == 0)
102 for (i = limb_index + 1; i < dsize; i++)
108 /* We got carry all way out beyond the end of D. Increase
109 its size (and allocation if necessary). */
111 if (d->alloc < dsize)
113 _mpz_realloc (d, dsize);