2 * bits.h -- bit vector manipulation
4 * Copyright (C) 2010,2011 Olaf Bergmann <bergmann@tzi.org>
6 * This file is part of the CoAP library libcoap. Please see
7 * README for terms of use.
12 * @brief bit vector manipulation
18 #ifdef HAVE_SYS_TYPES_H
19 #include <sys/types.h>
24 * Sets the bit @p bit in bit-vector @p vec. This function returns @c
25 * 1 if bit was set or @c -1 on error (i.e. when the given bit does
26 * not fit in the vector).
28 * @param vec The bit-vector to change.
29 * @param size The size of @p vec in bytes.
30 * @param bit The bit to set in @p vec.
32 * @return @c -1 if @p bit does not fit into @p vec, @c 1 otherwise.
34 inline static int bits_setb(uint8_t *vec, size_t size, uint8_t bit)
36 if (size <= (bit >> 3))
39 *(vec + (bit >> 3)) |= (uint8_t)(1 << (bit & 0x07));
44 * Clears the bit @p bit from bit-vector @p vec. This function returns
45 * @c 1 if bit was cleared or @c -1 on error (i.e. when the given bit
46 * does not fit in the vector).
48 * @param vec The bit-vector to change.
49 * @param size The size of @p vec in bytes.
50 * @param bit The bit to clear from @p vec.
52 * @return @c -1 if @p bit does not fit into @p vec, @c 1 otherwise.
54 inline static int bits_clrb(uint8_t *vec, size_t size, uint8_t bit)
56 if (size <= (bit >> 3))
59 *(vec + (bit >> 3)) &= (uint8_t)(~(1 << (bit & 0x07)));
64 * Gets the status of bit @p bit from bit-vector @p vec. This function returns
65 * @c 1 if the bit is set, @c 0 otherwise (even in case of an error)..
67 * @param vec The bit-vector to read from..
68 * @param size The size of @p vec in bytes.
69 * @param bit The bit to get from @p vec.
71 * @return @c 1 if the bit is set, @c 0 otherwise.
73 inline static int bits_getb(const uint8_t *vec, size_t size, uint8_t bit)
75 if (size <= (bit >> 3))
78 return (*(vec + (bit >> 3)) & (1 << (bit & 0x07))) != 0;