luc107.c (795B)
1 /* Receive an unsigned 16-bit integer and exchange the contents of its 2 bytes using bitwise operators. 2 */ 3 /* Let Us C, Chap- 21 (Operations on Bits), Qn No.: B(e) */ 4 5 /* This file is auto-generated by a bot. */ 6 /* This code is not compiled; it is for reference only. */ 7 8 9 #include <stdio.h> 10 #include <stdlib.h> 11 12 int main() 13 { 14 unsigned short num, swapped; 15 16 printf("Enter a 16-bit number (0-65535): "); 17 scanf("%hu", &num); 18 19 // Exchange bytes: 20 // 1. (num & 0xFF00) >> 8 : Move High Byte to Low Byte position 21 // 2. (num & 0x00FF) << 8 : Move Low Byte to High Byte position 22 23 swapped = ((num & 0xFF00) >> 8) | ((num & 0x00FF) << 8); 24 25 printf("Original: %hu (Hex: 0x%04X)\n", num, num); 26 printf("Swapped: %hu (Hex: 0x%04X)\n", swapped, swapped); 27 28 return 0; 29 }