|
| 1 | +/**************************************************************************** |
| 2 | + * |
| 3 | + * Copyright (c) 2023 IMProject Development Team. All rights reserved. |
| 4 | + * Authors: Juraj Ciberlin <jciberlin1@gmail.com> |
| 5 | + * |
| 6 | + * Redistribution and use in source and binary forms, with or without |
| 7 | + * modification, are permitted provided that the following conditions |
| 8 | + * are met: |
| 9 | + * |
| 10 | + * 1. Redistributions of source code must retain the above copyright |
| 11 | + * notice, this list of conditions and the following disclaimer. |
| 12 | + * 2. Redistributions in binary form must reproduce the above copyright |
| 13 | + * notice, this list of conditions and the following disclaimer in |
| 14 | + * the documentation and/or other materials provided with the |
| 15 | + * distribution. |
| 16 | + * 3. Neither the name IMProject nor the names of its contributors may be |
| 17 | + * used to endorse or promote products derived from this software |
| 18 | + * without specific prior written permission. |
| 19 | + * |
| 20 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 | + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 | + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 23 | + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 24 | + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 25 | + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 26 | + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS |
| 27 | + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
| 28 | + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 29 | + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
| 30 | + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 31 | + * POSSIBILITY OF SUCH DAMAGE. |
| 32 | + * |
| 33 | + ****************************************************************************/ |
| 34 | + |
| 35 | +#ifndef UTILITY_BIT_MANIPULATION_H_ |
| 36 | +#define UTILITY_BIT_MANIPULATION_H_ |
| 37 | + |
| 38 | +#include "typedefs.h" |
| 39 | + |
| 40 | +/** |
| 41 | + * @brief This function reflects the bits in data around the center bit. |
| 42 | + * |
| 43 | + * @param[in] data The input data to be reflected. |
| 44 | + * @param[in] n_bits The number of bits in the input data to be reflected. |
| 45 | + * |
| 46 | + * @return The reflected data. |
| 47 | + */ |
| 48 | +uint32_t BitManipulation_reflect(uint32_t data, uint8_t n_bits); |
| 49 | + |
| 50 | +/** |
| 51 | + * @brief This function checks if the bit at nth position is set. Parameter n must be between 0 and 31. |
| 52 | + * |
| 53 | + * @param[in] data The input data. |
| 54 | + * @param[in] n The position of the bit in the input data that will be checked. |
| 55 | + * @param[out] *bit_set True if bit at nth position is set, otherwise false. |
| 56 | + * |
| 57 | + * @return True if function is successfully performed, otherwise false. |
| 58 | + */ |
| 59 | +bool BitManipulation_bitSet(uint32_t data, uint8_t n, bool* bit_set); |
| 60 | + |
| 61 | +/** |
| 62 | + * @brief This function sets the bit at nth position and returns the result. Parameter n must be between 0 and 31. |
| 63 | + * |
| 64 | + * @param[in] data The input data. |
| 65 | + * @param[in] n The position of the bit in the input data that will be set. |
| 66 | + * @param[out] *out The result of the input data where the bit at the nth position is set. |
| 67 | + * |
| 68 | + * @return True if function is successfully performed, otherwise false. |
| 69 | + */ |
| 70 | +bool BitManipulation_setBit(uint32_t data, uint8_t n, uint32_t* out); |
| 71 | + |
| 72 | +/** |
| 73 | + * @brief This function clears the bit at nth position and returns the result. Parameter n must be between 0 and 31. |
| 74 | + * |
| 75 | + * @param[in] data The input data. |
| 76 | + * @param[in] n The position of the bit in the input data that will be cleared. |
| 77 | + * @param[out] *out The result of the input data where the bit at the nth position is cleared. |
| 78 | + * |
| 79 | + * @return True if function is successfully performed, otherwise false. |
| 80 | + */ |
| 81 | +bool BitManipulation_clearBit(uint32_t data, uint8_t n, uint32_t* out); |
| 82 | + |
| 83 | +/** |
| 84 | + * @brief This function toggles the bit at nth position and returns the result. Parameter n must be between 0 and 31. |
| 85 | + * |
| 86 | + * @param[in] data The input data. |
| 87 | + * @param[in] n The position of the bit in the input data that will be toggled. |
| 88 | + * @param[out] *out The result of the input data where the bit at the nth position is toggled. |
| 89 | + * |
| 90 | + * @return True if function is successfully performed, otherwise false. |
| 91 | + */ |
| 92 | +bool BitManipulation_toggleBit(uint32_t data, uint8_t n, uint32_t* out); |
| 93 | + |
| 94 | +#endif /* UTILITY_BIT_MANIPULATION_H_ */ |
0 commit comments