luc101.c (1338B)
1 /* Decode student information (Year, Stream, Room No) packed into an integer array. 2 */ 3 /* Let Us C, Chap- 21 (Operations on Bits), Qn No.: A(c) */ 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 int data[] = {273, 548, 786, 1096}; 15 int n = 4, i; 16 int val, year, stream_bits, room; 17 18 for (i = 0; i < n; i++) 19 { 20 val = data[i]; 21 22 // Room Number: Rest of the bits (Assuming starting from bit 8) 23 room = val >> 8; 24 25 printf("Student %d (Raw: %d):\n", i + 1, val); 26 printf(" Room No: %d\n", room); 27 28 // Year: Bits 0-3 29 printf(" Year: "); 30 if (val & (1 << 0)) printf("1st Year"); 31 else if (val & (1 << 1)) printf("2nd Year"); 32 else if (val & (1 << 2)) printf("3rd Year"); 33 else if (val & (1 << 3)) printf("4th Year"); 34 else printf("Unknown"); 35 printf("\n"); 36 37 // Stream: Bits 4-7 (Mech, Chem, Elec, CS) 38 printf(" Stream: "); 39 if (val & (1 << 4)) printf("Mechanical"); 40 else if (val & (1 << 5)) printf("Chemical"); 41 else if (val & (1 << 6)) printf("Electronics"); 42 else if (val & (1 << 7)) printf("CS"); 43 else printf("Unknown"); 44 printf("\n\n"); 45 } 46 47 return 0; 48 }