CIS 2107‎ > ‎

Lab 4 - BitWise Operators

Description

Complete all of the unfinished functions and rewrite add( ), sub( ), and mul( ) so that each computes the correct result using only the "bitwise" operators (&, |, ^, <<, >>, ~) along with !, =, ==, !=, && and ||.

The code

#include <stdio.h>

unsigned int add(unsigned int, unsigned int);
unsigned int sub(unsigned int, unsigned int);
unsigned int mul(unsigned int, unsigned int);
unsigned int reverse(unsigned int);
int evens(unsigned int);
int is_negative(int);
unsigned int pack_date(int, int, int);
int getMonth(unsigned int);
int getDay(unsigned int);
int getYear(unsigned int);

int main(int argc, char **argv)
{
  unsigned int i, j;
  int x, y;
  unsigned int year, month, day, date;

  printf("Enter an integer: ");
  scanf("%u", &i);
  printf("Enter another integer: ");
  scanf("%u", &j);
  printf("One more integer, please: ");
  scanf("%d", &x);
  printf("Just one more integer, please: ");
  scanf("%d", &y);

  if (is_negative(x))
    printf("%d is negative\n", x);
  else
    printf("%d isn't negative\n", x);

  printf("%d and %d have ", x, y);
  if (opposite_signs(x, y))
    printf("opposite signs.\n");
  else
    printf("the same sign.\n");

  printf("i + j = %u\n", add(i,j));
  printf("i - j = %u\n", sub(i,j));
  printf("i * j = %u\n", mul(i,j));
  printf("%x in reverse is %x\n", i, reverse(i));
  if (evens(i))
    printf("There are an even number of 1s in %d\n", i);
  else
    printf("There are an odd number of 1s in %d\n", i);

  printf("Enter a year: ");
  scanf("%u", &year);
  
  printf("Enter a month as an integer (1-12): ");
  scanf("%u", &month);
  
  printf("Enter a day as an integer (1-31): ");
  scanf("%u", &day);

  date = pack_date(year, month, day);

  printf("You entered: %d/%d/%d\n", getMonth(date),
	 getDay(date), getYear(date));

  return 0;
}

unsigned int add(unsigned int i, unsigned int j) {
 unsigned int carry = i & j;
unsigned int result = i ^ j;
while(carry != 0)
{
unsigned int shiftedcarry = carry << 1;
carry = result & shiftedcarry;
result ^= shiftedcarry;
}
return result; } unsigned int sub(unsigned int i, unsigned int j) {  
j = add(~j, 1);
return add(i,j);
}

unsigned int mul(unsigned int i, unsigned int j) {
	unsigned int c = 0;
	while(j){
		if(j&1)
			c = add(c, i);
		i <<= 1;
		j>>=1;
	}
	return c;
}


unsigned int reverse(unsigned int i) {
	unsigned int j = 0;
	int num = (int)mul(sizeof(int),8); /* Get number of bits */
	while(num>0){
		if(i&1)
			j ^= 1;
		i>>=1;
		num=sub(num,1);
		if(num>0)
			j<<=1;
	}
	return j;
}

/* returns 1 if x < 0, returns 0 otherwise */
int is_negative(int x) {
	int a = mul(sizeof(int),8);
	while(a>1){
		x >>= 1;	
		a=sub(a,1);
	}
	if(x&1)
		return 1;
	return 0;
}

/* returns 1 if there are an even number of 1's in x's binary representation or 0 otherwise */
int evens(unsigned int x) {
	int a;
	while(x){
		if(x&1)
			a=add(a,1);
		x>>=1;
	}
	if(a&1)
		return 0;
	return 1;
}

/* returns 1 if x and y have opposite sign (i.e., one is negative and the other is non-negative) or 0 otherwise */
int opposite_signs(int x, int y) {
  return (x&0x80000000)^(y&0x80000000);
}


/* packs a date as year, month, day into a single unsigned int with:
   - the day stored as the 5 low-order bits,
   - the month as the next 4 bits, and 
   - the year as the next 12 bits.

   For example if the date is July 4, 1776:
   - day is 4, i.e., 0 0100 
   - month is 7, i.e. 0111
   - year is 1776, i.e. 110 1111 0000

   The function would therefore return the integer:
   0x000DE0E4, i.e.:
   0000 0000 0000 1101 1110 0000 1110 0100 
*/
unsigned int pack_date(int year, int month, int day) {
unsigned int temp = 0;
temp = temp | year;
temp<<=4;
temp = temp|month;
temp<<=5;
temp=temp|day;
return temp;
}

/* given a date stored in the format described above, returns the month stored as an int */
int getMonth(unsigned int date) {
unsigned int month = 0x000001e0;
date = date & month;
date >>=5;
  return date;
}

/* given a date stored in the format described above, returns the day stored as an int */
int getDay(unsigned int date) {
int day = 0x0000001f;
  return date & day;
}

/* given a date stored in the format described above, returns the year stored as an int */
int getYear(unsigned int date) {
int year = 0x001FFe00;
year = date & year;
year >>=9;
  return year;
}
ċ
bits.c
(4k)
Mark Dolan,
Sep 24, 2014, 5:03 PM
Comments