bsc

Comprehensive codebase and coursework for the 4-year B.Sc. Computer Science Honours degree (WBSU)

git clone https://github.com/notamitgamer/bsc.git
· rebuilt hourlyOpen on GitHub
root / semester_1 / letusc / luc108.c

luc108.c (713B)


      1 /* Receive an 8-bit number and exchange its higher 4 bits with lower 4 bits.
      2 */
      3 /* Let Us C, Chap- 21 (Operations on Bits), Qn No.: B(f) */
      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 char num, swapped;
     15 
     16     printf("Enter an 8-bit number (0-255): ");
     17     scanf("%hhu", &num);
     18 
     19     // Exchange nibbles
     20     // (num & 0xF0) >> 4 : High nibble to Low
     21     // (num & 0x0F) << 4 : Low nibble to High
     22 
     23     swapped = ((num & 0xF0) >> 4) | ((num & 0x0F) << 4);
     24 
     25     printf("Original: %d (Hex: 0x%02X)\n", num, num);
     26     printf("Swapped:  %d (Hex: 0x%02X)\n", swapped, swapped);
     27 
     28     return 0;
     29 }