Become a leader in the IoT community!
New DevHeads get a 320-point leaderboard boost when joining the DevHeads IoT Integration Community. In addition to learning and advising, active community leaders are rewarded with community recognition and free tech stuff. Start your Legendary Collaboration now!
Ah, but you have to limit the length of the values to be complemented. (& 0xff, etc…)
||“`C
C
#include
unsigned int swapOddEvenBits(unsigned int num) {
// Masks for even and odd bits
unsigned int evenMask = 0b10101010101010101010101010101010;
unsigned int oddMask = 0b01010101010101010101010101010101;
// Extract even and odd bits
unsigned int evenBits = num & evenMask;
unsigned int oddBits = num & oddMask;
// Swap even and odd bits
evenBits >>= 1;
oddBits <<= 1; // Combine swapped even and odd bits return evenBits | oddBits; } int main() { // Example input: 10101010 unsigned int num = 0b10101010; // Print input printf("Input: %08x\n", num); // Swap odd and even bits unsigned int result = swapOddEvenBits(num); // Print output printf("Output: %08x\n", result); return 0; } ```||
CONTRIBUTE TO THIS THREAD