Chapter - 15: Operations On Bits

In order to save disk space information about the student is stored in an integer variable. If bit number 0 is on then it indicates Ist year student, bit number 1 to 3 stores IInd year, IIIrd year and IVth year student respectively. The bit number 4 to 7 stores stream Mechanical, Chemical, Electronics and IT. Rest of the bits store room number. Based on the given data, write a program that asks for the room number and displays the information about the student, if its data exists in the array.


A
Sections
6
Exercises

The contents of the array are,

int data[ ] = { 273, 548, 786, 1096 } ;


#include<stdio.h>
#include<conio.h>
#include<math.h>

#define _BV(x) (1<<x)

void showinfo(int);

int main()
{
	int rnum, search;
	int data[] = { 273, 548, 786, 1096 };
	printf("\nEnter the room number : ");
	scanf("%d", &rnum);

	for (int i = 0; i < 4; i++)
	{
		search = 0;
		for (int j = 8; j < 16; j++)
			if (data[i] & _BV(j))
				search += int(pow(2, j - 8));
		if (rnum == search)
		{
			showinfo(data[i]);
			break;
		}
	}
	_getch();
	return 0;
}

void showinfo(int a)
{
	int search = 0;
	for (int i = 0; i < 16; i++)
	{
		//If a non zero bit is encountered
		if (a & _BV(i))
		{
			if (i < 4)
			{
				printf("\n\nYear: ");
				switch(i)
				{
				case 0:
					printf("First\n");
					break;
				case 1:
					printf("Second\n");
					break;
				case 2:
					printf("Thrid\n");
					break;
				case 3:
					printf("Fourth\n");
				default:;
				}
			}
			else if (i > 7)
				/*calculating room number*/
				search += int(pow(2, i - 8));
			else
			{
				printf("Branch: ");
				switch (i)
				{
				case 4:
					printf("Mechanical\n");
					break;
				case 5:
					printf("Chemical\n");
					break;
				case 6:
					printf("Electronics\n");
					break;
				case 7:
					printf("IT\n");
				default:;

				}
			}
		}
	}
	printf("Room Number: %d\n", search);
}

© 2021 Garbage Valuegarbage value logo