aboutsummaryrefslogtreecommitdiffstats
path: root/src/zbset.c
blob: 33252c1d02aac963230f5a3af9a01607da3716db (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/* See LICENSE file for copyright and license details. */
#include "internals.h"


void
zbset(z_t a, z_t b, size_t bit, int action)
{
	zahl_char_t mask = 1;
	size_t chars;

	chars = FLOOR_BITS_TO_CHARS(bit);
	bit = BITS_IN_LAST_CHAR(bit);
	mask <<= bit;

	SET(a, b);

	if (action) {
		if (zzero(a)) {
			a->used = 0;
			SET_SIGNUM(a, 1);
		}
		if (a->used <= chars) {
			ENSURE_SIZE(a, chars + 1);
			zmemset(a->chars + a->used, 0, chars + 1 - a->used);
			a->used = chars + 1;
		}
	}

	if (action > 0) {
		a->chars[chars] |= mask;
		return;
	} else if (action < 0) {
		a->chars[chars] ^= mask;
	} else if (chars < a->used) {
		a->chars[chars] &= ~mask;
	}

	TRIM(a);
	if (!a->used)
		SET_SIGNUM(a, 0);
}