Ad

Chapter - 15: Operations On Bits

In an inter-college competition, various sports and games are played between different colleges like cricket, basketball, football, hockey, lawn tennis, table tennis, carom, and chess. The information regarding the games won by a particular college is stored in bit numbers 0, 1, 2, 3, 4, 5, 6, 7 and 8 respectively of an integer variable called the game. The college that wins in 5 or more than 5 games is awarded the Champion of Champions trophy. If a number is entered through the keyboard, then write a program to find out whether the college won the Champion of the Champions Trophy or not, along with the names of the games won by the college.


Ad
A
Sections
3
Exercises
#include<stdio.h>
#include<conio.h>
#include<windows.h>
#include<math.h>

#define _BV(x) 1<<x

int bintodec(int*);
void initinfo(int*);
void addinfo(int *);

int main()
{
	int game, i, count = 0;
	unsigned int andmask, j;
	addinfo(&game);
	for (i = 0; i <= 8; i++)
	{
		andmask = _BV(i);
		j = game & andmask;
		if (j == andmask)
			count++;
	}
	if (count >= 5)
		printf("\nYou are eligible for champions trophy.\n");
	else
		printf("\nYou aren't eligible for champions trophy.\n");
	_getch();
	return 0;
}

int bintodec(int *num)
{
	int i, dec = 0;
	for (i = 0; i <= 8; i++)
		dec += num[i] * (int)pow(2, 8 - i);
	return dec;
}

void initinfo(int *information)
{
	int i;
	for (i = 0; i <= 8; i++)
		information[i] = 0;
}

void addinfo(int *game)
{
	int info[9], i;
	printf("1. Cricket\n2. Basketball\n");
	printf("3. Football\n4.Hockey\n");
	printf("5.Lawn Tennis\n6.Table Tennis\n");
	printf("7.Carom\n8.Chess\n9.Kabaddi");
	printf("\nEnter the number of winning of games.");
	printf("Answer given should be in the form of 0 or 1.");
	printf("And should be given\n one by one for each game.\n");
	for (i = 0; i <= 8; i++)
		scanf("%d", &info[i]);
	*game = bintodec(info);
}
Ad

© 2024 Garbage Valuegarbage value logo