Data Lab Answers
Data Lab Answers
/*
* Casey Burkhardt
* Kristin Raudonis
* bits.c - Contains the solutions for the Data Lab assignment.
*/
#include "btest.h"
#include <limits.h>
// Team Information Block
team_struct team =
{
/* Team name: Replace with either:
Your login ID if working as a one person team
or, ID1+ID2 where ID1 is the login ID of the first team member
and ID2 is the login ID of the second team member */
"cburkhar+kraudoni",
/* Student name 1: Replace with the full name of first team member */
"Casey Burkhardt",
/* Login ID 1: Replace with the login ID of first team member */
"cburkhar",
/* The following should only be changed if there are two team members */
/* Student name 2: Full name of the second team member */
"Kristin Raudonis",
/* Login ID 2: Login ID of the second team member */
"kraudoni"
};
// Problem / Solution Set
/*
* bitNor - ~(x|y) using only ~ and &
*
Example: bitNor(0x6, 0x5) = 0xFFFFFFF8
*
Legal ops: ~ &
*
Max ops: 8
*
Rating: 1
*/
int bitNor(int x, int y)
{
// Applied DeMorgan's Law to expression:
// ~(x|y)
return (~x & ~y);
}
/*
* bitXor - x^y using only ~ and &
*
Example: bitXor(4, 5) = 1
*
Legal ops: ~ &
*
Max ops: 14
*
Rating: 2
*/
int bitXor(int x, int y)
{
// Applied DeMorgan's Law to expression:
// (~x & y) | (x & ~y)
http://caseyburkhardt.com/classes/csc2400/projects/DataLab/bits.c
Page 1 of 7
9/8/15, 6:24 PM
Page 2 of 7
9/8/15, 6:24 PM
*
Rating: 3
*/
int logicalShift(int x, int n)
{
// (x >> n) computes the arithmetic right shift of x by n.
// ((1 << ((~n + 1) + 32)) + ~0) computes a dynamic mask that allows for
arithmetically shifted bits to be converted to logical when necessary.
return ((x >> n) & ((1 << ((~n + 1) + 32)) + ~0));
}
/*
* bitCount - returns count of number of 1's in word
*
Examples: bitCount(5) = 2, bitCount(7) = 3
*
Legal ops: ! ~ & ^ | + << >>
*
Max ops: 40
*
Rating: 4
*/
int bitCount(int x)
{
// Mask 1 encompasses the 2 least significant bytes
int mask1 = 0x11 | (0x11 << 8);
// Mask 2 encompasses the final bytes
int mask2 = mask1 | (mask1 << 16);
// Sum will
// Computes
int sum = x
sum = sum +
sum = sum +
sum = sum +
// At this point, sum represents the number of 1 bits within the first 4 bits.
// in addition to extraneous bits beyond the first four bits.
// As the binary position of these values do not represent their appropriate value
in relation to the sum, they must be stripped.
// Adjusts for overestimated sum value due to addition of 1 bits beyond first four
bits.
sum = sum + (sum >> 16);
// Used to preserve current sum, and continue to mask 1 bits in the next byte.
mask1 = 0xF | (0xF << 8);
// Alternates the preserved bits of sum and adds alternating 4 bits together.
sum = (sum & mask1) + ((sum >> 4) & mask1);
// Shift sum value 1 byte and implement mask to limit resulting sum to 6 bits
// Maximum representation of 6 bits, or a decimal value of 32, the word size for
this problem set.
return((sum + (sum >> 8)) & 0x3F);
/* Funny Solution
int mask = 0x01;
int sum = 0;
sum = sum + (x & mask);
http://caseyburkhardt.com/classes/csc2400/projects/DataLab/bits.c
Page 3 of 7
9/8/15, 6:24 PM
sum = sum + (x
sum = sum + (x
sum = sum + (x
sum = sum + (x
sum = sum + (x
sum = sum + (x
sum = sum + (x
sum = sum + (x
sum = sum + (x
sum = sum + (x
sum = sum + (x
sum = sum + (x
sum = sum + (x
sum = sum + (x
sum = sum + (x
sum = sum + (x
sum = sum + (x
sum = sum + (x
sum = sum + (x
sum = sum + (x
sum = sum + (x
sum = sum + (x
sum = sum + (x
sum = sum + (x
sum = sum + (x
sum = sum + (x
sum = sum + (x
sum = sum + (x
sum = sum + (x
sum = sum + (x
sum = sum + (x
return(sum);
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
1 & mask);
2 & mask);
3 & mask);
4 & mask);
5 & mask);
6 & mask);
7 & mask);
8 & mask);
9 & mask);
10 & mask);
11 & mask);
12 & mask);
13 & mask);
14 & mask);
15 & mask);
16 & mask);
17 & mask);
18 & mask);
19 & mask);
20 & mask);
21 & mask);
22 & mask);
23 & mask);
24 & mask);
25 & mask);
26 & mask);
27 & mask);
28 & mask);
29 & mask);
30 & mask);
31 & mask);
*/
}
/*
* bang - Compute !x without using !
*
Examples: bang(3) = 0, bang(0) = 1
*
Legal ops: ~ & ^ | + << >>
*
Max ops: 12
*
Rating: 4
*/
int bang(int x)
{
// The logical negative value of x.
int negative_x = ~x + 1;
// If (x != 0), then the most significant bit (or sign bit) of either x or -x will
be 1.
// XOR the sign bit of x or -x with the mask 0x01 to reproduce functionality of !
return((((x >> 31) & 0x01) | ((negative_x >> 31) & 0x01)) ^ 0x01);
}
/*
* leastBitPos - return a mask that marks the position of the
*
least significant 1 bit. If x == 0, return 0
*
Example: leastBitPos(96) = 0x20
http://caseyburkhardt.com/classes/csc2400/projects/DataLab/bits.c
Page 4 of 7
9/8/15, 6:24 PM
*
Legal ops: ! ~ & ^ | + << >>
*
Max ops: 6
*
Rating: 4
*/
int leastBitPos(int x)
{
// The logical negative value of x.
int negative_x = ~x + 1;
// x and the logical negative value of x combined with the & operator properly
produce the desired mask.
return(x & negative_x);
}
/*
* TMax - return maximum two's complement integer
*
Legal ops: ! ~ & ^ | + << >>
*
Max ops: 4
*
Rating: 1
*/
int tmax(void)
{
// Constant representing binary value: 1000 0000
int x = 0x80;
// The compliment of the binary value 1000 0000 shifted 24 bits to the left
produces the following binary value:
// 0111 1111 1111 1111 1111 1111 1111 1111
// This is the largest 32-bit two's compliment integer
return(~(x << 24));
}
/*
* isNonNegative - return 1 if x >= 0, return 0 otherwise
*
Example: isNonNegative(-1) = 0. isNonNegative(0) = 1.
*
Legal ops: ! ~ & ^ | + << >>
*
Max ops: 6
*
Rating: 3
*/
int isNonNegative(int x)
{
// Boolean value indicating sign of x
// 1 = Negative
// 0 = Non-Negative
int sign_x = x >> 31;
// The negation of the sign bit of value x computes the appropriate boolean return
value.
return (!(sign_x));
}
/*
* isGreater - if x > y then return 1, else return 0
*
Example: isGreater(4,5) = 0, isGreater(5,4) = 1
*
Legal ops: ! ~ & ^ | + << >>
*
Max ops: 24
http://caseyburkhardt.com/classes/csc2400/projects/DataLab/bits.c
Page 5 of 7
9/8/15, 6:24 PM
*
Rating: 3
*/
int isGreater(int x, int y)
{
// Boolean value indicating sign of x
// 1 = Negative
// 0 = Non-Negative
int sign_x = x >> 31;
// Boolean value indicating sign of y
// 1 = Negative
// 0 = Non-Negative
int sign_y = y >> 31;
// if the signs are equal, then
// if x is larger, sign bit of (~y + x) is 0
// if y is larger, sign bit of (~y + x) is 1
int equal = !(sign_x ^ sign_y) & ((~y + x) >> 31);
// if signs are not equal, these principles are reversed.
int notEqual = sign_x & !sign_y;
// this | returns 0 when it is x is greater, so you have to negate it.
return !( equal | notEqual);
}
/*
* divpwr2 - Compute x/(2^n), for
* Round toward zero
*
Examples: divpwr2(15,1) = 7,
*
Legal ops: ! ~ & ^ | + << >>
*
Rating: 2
*/
int divpwr2(int x, int n)
{
// Something is needed to
negative
0 <= n <= 30
divpwr2(-33,4) = -2
*
Max ops: 15
Page 6 of 7
9/8/15, 6:24 PM
int abs(int x)
{
// Boolean value indicating sign of x
// 1 = Negative
// 0 = Non-Negative
int sign_x = x >> 31;
// XOR of value x and the sign bit of value x plus 1 plus the compliment of the
sign bit of x returns the absolute value of x.
return((x ^ (sign_x)) + (1 + ( ~(sign_x))));
}
/*
* addOK - Determine if can compute x+y without overflow
*
Example: addOK(0x80000000,0x80000000) = 0,
*
addOK(0x80000000,0x70000000) = 1,
*
Legal ops: ! ~ & ^ | + << >>
*
Max ops: 20
*
Rating: 3
*/
int addOK(int x, int y)
{
// The sum of x and y
int xy_sum = x + y;
// Boolean value indicating sign of x
// 1 = Negative
// 0 = Non-Negative
int sign_x = x >> 31;
// Boolean value indicating sign of y
// 1 = Negative
// 0 = Non-Negative
int sign_y = y >> 31;
// Boolean value indicating sign of the sum of x and y
// 1 = Negative
// 0 = Non-Negative
int sign_sum_xy = xy_sum >> 31;
// An overflow occurs when the sign of x and y are the same, but the sign of the
sum of x and y is different
return !(~(sign_x ^ sign_y) & (sign_x ^ sign_sum_xy));
}
http://caseyburkhardt.com/classes/csc2400/projects/DataLab/bits.c
Page 7 of 7