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 #include <sys/types.h>
22 * Sets the bit @p bit in bit-vector @p vec. This function returns @c
23 * 1 if bit was set or @c -1 on error (i.e. when the given bit does
24 * not fit in the vector).
26 * @param vec The bit-vector to change.
27 * @param size The size of @p vec in bytes.
28 * @param bit The bit to set in @p vec.
30 * @return @c -1 if @p bit does not fit into @p vec, @c 1 otherwise.
33 bits_setb(uint8_t *vec, size_t size, uint8_t bit) {
34 if (size <= (bit >> 3))
37 *(vec + (bit >> 3)) |= (uint8_t)(1 << (bit & 0x07));
42 * Clears the bit @p bit from bit-vector @p vec. This function returns
43 * @c 1 if bit was cleared or @c -1 on error (i.e. when the given bit
44 * does not fit in the vector).
46 * @param vec The bit-vector to change.
47 * @param size The size of @p vec in bytes.
48 * @param bit The bit to clear from @p vec.
50 * @return @c -1 if @p bit does not fit into @p vec, @c 1 otherwise.
53 bits_clrb(uint8_t *vec, size_t size, uint8_t bit) {
54 if (size <= (bit >> 3))
57 *(vec + (bit >> 3)) &= (uint8_t)(~(1 << (bit & 0x07)));
62 * Gets the status of bit @p bit from bit-vector @p vec. This function returns
63 * @c 1 if the bit is set, @c 0 otherwise (even in case of an error)..
65 * @param vec The bit-vector to read from..
66 * @param size The size of @p vec in bytes.
67 * @param bit The bit to get from @p vec.
69 * @return @c 1 if the bit is set, @c 0 otherwise.
72 bits_getb(const uint8_t *vec, size_t size, uint8_t bit) {
73 if (size <= (bit >> 3))
76 return (*(vec + (bit >> 3)) & (1 << (bit & 0x07))) != 0;