2 * RPP binary handling header file.
3 * This file contains helpful macros for binary and bit manipulation.
7 * @copyright Copyright (C) 2013 Czech Technical University in Prague
9 * @author Carlos Jenkins <carlos@jenkins.co.cr>
12 #ifndef __RPP_BINARY_H
13 #define __RPP_BINARY_H
15 // Discussion about binary macros here:
16 // http://www.avrfreaks.net/index.php?name=PNphpBB2&file=viewtopic&t=60729
19 * Create a bit mask for given bit number.
21 * - _BV(7) -> B[0100 0000]
22 * - _BV(1) -> B[0000 0010]
23 * - _BV(0) -> B[0000 0001]
25 * The function name stands for Bit Value, in reference that the mask is created
26 * only with the given bit number set.
28 * @param[in] bit Bit number to create the mask, starting at 0 for the
29 * least-significant (rightmost) bit.
31 * @return A mask with all bits clear except the given bit number.
33 #define _BV(bit) (1UL << (bit))
36 * Reads a bit of a number.
38 * @param[out] value The number from which to read.
39 * @param[in] bit Which bit to read, starting at 0 for the
40 * least-significant (rightmost) bit.
42 * @return The value of the bit (0 or 1).
44 #define bit_read(value, bit) (((value) >> (bit)) & 0x01)
48 * Sets (writes a 1 to) a bit of a numeric variable.
50 * @param[out] value The numeric variable whose bit to set.
51 * @param[in] bit Which bit to set, starting at 0 for the
52 * least-significant (rightmost) bit
56 #define bit_set(value, bit) ((value) |= (1UL << (bit)))
60 * Clears (writes a 0 to) a bit of a numeric variable.
62 * @param[out] value The numeric variable whose bit to clear.
63 * @param[in] bit Which bit to clear, starting at 0 for the
64 * least-significant (rightmost) bit.
68 #define bit_clear(value, bit) ((value) &= ~(1UL << (bit)))
72 * Writes a bit of a numeric variable.
74 * @param[out] value The numeric variable to which to write.
75 * @param[in] bit Which bit of the number to write, starting at 0 for the
76 * least-significant (rightmost) bit.
77 * @param[in] bit_value The value to write to the bit (0 or 1).
81 #define bit_write(value, bit, bit_value) (bit_value ? bit_set(value, bit) : bit_clear(value, bit))
84 #endif /* __RPP_BINARY_H */