Code:

inline int get_bit_big_endian (uint32_t number, int bit_position)
{
const int bit_adjust = 32 - bit_position - 1;
return ((number >> bit_adjust) & 1);
}
inline uint32_t clear_bit_big_endian (uint32_t number, int bit_position)
{
const int bit_adjust = 32 - bit_position - 1;
return number & ~(((uint32_t)1) << bit_adjust);
}
inline uint32_t set_bit_big_endian (uint32_t number, int bit_position, uint32_t value = 1)
{
const int bit_adjust = 32 - bit_position - 1;
return clear_bit_big_endian (number, bit_position) | ((value & 1) << bit_adjust);
}
inline int get_bit_little_endian (uint32_t number, int bit_position)
{
return ((number >> bit_position) & 1);
}
inline uint32_t clear_bit_little_endian (uint32_t number, int bit_position)
{
return number & ~(((uint32_t)1) << bit_position);
}
inline uint32_t set_bit_little_endian (uint32_t number, int bit_position, uint32_t value = 1)
{
return clear_bit_little_endian (number, bit_position) | ((value & 1) << bit_position);
}
void setup ()
{
uint32_t five = 0b101;
Serial.printf ("get_be (0x%08x, 0) = 0x%08x, get_be (0x%08x, 31) = 0x%08x\n",
(unsigned) five,
(unsigned) get_bit_big_endian (five, 0),
(unsigned) five,
(unsigned) get_bit_big_endian (five, 31));
Serial.printf ("get_le (0x%08x, 0) = 0x%08x, get_le (0x%08x, 31) = 0x%08x\n",
(unsigned) five,
(unsigned) get_bit_little_endian (five, 0),
(unsigned) five,
(unsigned) get_bit_little_endian (five, 31));
Serial.printf ("set_be (0x%08x, 0, 1) = 0x%08x, set_be (0x%08x, 31, 1) = 0x%08x\n",
(unsigned) five,
(unsigned) set_bit_big_endian (five, 0, 1),
(unsigned) five,
(unsigned) set_bit_big_endian (five, 31, 1));
Serial.printf ("set_be (0x%08x, 0, 0) = 0x%08x, set_be (0x%08x, 31, 0) = 0x%08x\n",
(unsigned) five,
(unsigned) set_bit_big_endian (five, 0, 0),
(unsigned) five,
(unsigned) set_bit_big_endian (five, 31, 0));
Serial.printf ("set_le (0x%08x, 0, 1) = 0x%08x, set_le (0x%08x, 31, 1) = 0x%08x\n",
(unsigned) five,
(unsigned) set_bit_little_endian (five, 0, 1),
(unsigned) five,
(unsigned) set_bit_little_endian (five, 31, 1));
Serial.printf ("set_le (0x%08x, 0, 0) = 0x%08x, set_le (0x%08x, 31, 0) = 0x%08x\n",
(unsigned) five,
(unsigned) set_bit_little_endian (five, 0, 0),
(unsigned) five,
(unsigned) set_bit_little_endian (five, 31, 0));
}
void loop ()
{
}

Would print:
Code:

get_be (0x00000005, 0) = 0x00000000, get_be (0x00000005, 31) = 0x00000001
get_le (0x00000005, 0) = 0x00000001, get_le (0x00000005, 31) = 0x00000000
set_be (0x00000005, 0, 1) = 0x80000005, set_be (0x00000005, 31, 1) = 0x00000005
set_be (0x00000005, 0, 0) = 0x00000005, set_be (0x00000005, 31, 0) = 0x00000004
set_le (0x00000005, 0, 1) = 0x00000005, set_le (0x00000005, 31, 1) = 0x80000005
set_le (0x00000005, 0, 0) = 0x00000004, set_le (0x00000005, 31, 0) = 0x00000005

Note, I ran this on my laptop that I post from, and then adjusted it for running on a Teensy.