1 /* mpz_clrbit -- clear a specified bit.
3 Copyright (C) 1991, 1993, 1994, 1995 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 Library General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or (at your
10 option) any later version.
12 The GNU MP Library is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
15 License for more details.
17 You should have received a copy of the GNU Library General Public License
18 along with the GNU MP Library; see the file COPYING.LIB. If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
20 MA 02111-1307, USA. */
27 mpz_clrbit (mpz_ptr d, unsigned long int bit_index)
29 mpz_clrbit (d, bit_index)
31 unsigned long int bit_index;
34 mp_size_t dsize = d->_mp_size;
38 limb_index = bit_index / BITS_PER_MP_LIMB;
41 if (limb_index < dsize)
43 dp[limb_index] &= ~((mp_limb_t) 1 << (bit_index % BITS_PER_MP_LIMB));
44 MPN_NORMALIZE (dp, dsize);
54 /* Simulate two's complement arithmetic, i.e. simulate
55 1. Set OP = ~(OP - 1) [with infinitely many leading ones].
57 3. Set OP = ~OP + 1. */
61 /* No upper bound on this loop, we're sure there's a non-zero limb
63 for (zero_bound = 0; ; zero_bound++)
64 if (dp[zero_bound] != 0)
67 if (limb_index > zero_bound)
69 if (limb_index < dsize)
70 dp[limb_index] |= (mp_limb_t) 1 << (bit_index % BITS_PER_MP_LIMB);
73 /* Ugh. The bit should be cleared outside of the end of the
74 number. We have to increase the size of the number. */
75 if (d->_mp_alloc < limb_index + 1)
77 _mpz_realloc (d, limb_index + 1);
80 MPN_ZERO (dp + dsize, limb_index - dsize);
81 dp[limb_index] = (mp_limb_t) 1 << (bit_index % BITS_PER_MP_LIMB);
82 d->_mp_size = -(limb_index + 1);
85 else if (limb_index == zero_bound)
87 dp[limb_index] = ((dp[limb_index] - 1)
88 | ((mp_limb_t) 1 << (bit_index % BITS_PER_MP_LIMB))) + 1;
89 if (dp[limb_index] == 0)
92 for (i = limb_index + 1; i < dsize; i++)
98 /* We got carry all way out beyond the end of D. Increase
99 its size (and allocation if necessary). */
101 if (d->_mp_alloc < dsize)
103 _mpz_realloc (d, dsize);
107 d->_mp_size = -dsize;