{{
/*
* Programming Assignment #1
*
* bits.c - Source file with your solutions to the assignment.
* This is the file you will hand in.
*
* WARNING: Do not include the <stdio.h> header; it confuses the dlc
* compiler. You can still use printf for debugging without including
* <stdio.h>, although you might get a compiler warning. In general,
* it's not good practice to ignore compiler warnings, but in this
* case it's OK.
*/
#include "btest.h"
#include <limits.h>
#if 0
/*
* Instructions to Students:
* STEP 1: Read the following instructions carefully.
*/
You will provide your solution to the assignment by
editing the collection of functions in this source file.
CODING RULES:
Replace the "return" statement in each function with one
or more lines of C code that implements the function. Your code
must conform to the following style:
int Funct(arg1, arg2, ...) {
/* brief description of how your implementation works */
int var1 = Expr1;
...
int varM = ExprM;
varJ = ExprJ;
...
varN = ExprN;
return ExprR;
}
Each "Expr" is an expression using ONLY the following:
1. Integer constants 0 through 255 (0xFF), inclusive. You are
not allowed to use big constants such as 0xffffffff.
2. Function arguments and local variables (no global variables).
3. Unary integer operations ! ~
4. Binary integer operations & ^ | + << >>
Some of the problems restrict the set of allowed operators even further.
Each "Expr" may consist of multiple operators. You are not restricted to
one operator per line.
You are expressly forbidden to:
1. Use any control constructs such as if, do, while, for, switch, etc.
2. Define or use any macros.
3. Define any additional functions in this file.
4. Call any functions.
5. Use any other operations, such as &&, ||, -, or ?:
6. Use any form of casting.
You may assume that your machine:
1. Uses 2s complement, 32-bit representations of integers.
2. Performs right shifts arithmetically.
3. Has unpredictable behavior when shifting an integer by more
than the word size.
EXAMPLES OF ACCEPTABLE CODING STYLE:
/*
* pow2plus1 - returns 2^x + 1, where 0 <= x <= 31
*/
int pow2plus1(int x) {
/* exploit ability of shifts to compute powers of 2 */
return (1 << x) + 1;
}
/*
* pow2plus4 - returns 2^x + 4, where 0 <= x <= 31
*/
int pow2plus4(int x) {
/* exploit ability of shifts to compute powers of 2 */
int result = (1 << x);
result += 4;
return result;
}
NOTES:
1. Use the dlc compiler (described in the handout) to
check the legality of your solutions.
2. Each function has a maximum number of operators (! ~ & ^ | + << >>)
that you are allowed to use for your implementation of the function.
The max operator count is checked by dlc. Note that '=' is not
counted; you may use as many of these as you want without penalty.
3. Use the btest test harness to check your functions for correctness.
4. The maximum number of ops for each function is given in the
header comment for each function. If there are any inconsistencies
between the maximum ops in the writeup and in this file, consider
this file the authoritative source.
#endif
/*
* STEP 2: Modify the following functions according the coding rules.
*
* IMPORTANT. TO AVOID GRADING SURPRISES:
* 1. Use the dlc compiler to check that your solutions conform
* to the coding rules.
* 2. Use the btest test harness to check that your solutions produce
* the correct answers. Watch out for corner cases around Tmin and Tmax.
*/
/*
* bitNor - ~(x|y) using only ~ and &
* Example: bitNor(0x6, 0x5) = 0xFFFFFFF8
* Legal ops: ~ &
* Max ops: 8
* Rating: 1
*/
int bitNor(int x, int 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) {
/*
* 원래의 XOR 식인 (x & ~y) | (~x & y) 에서 변환
* NAND 와 NOR을 연결하여 OR 의 기능을 수행
* ~(~A & ~B ) = ~(~A) | ~(~B) = A + B
*/
return ~( ~(x & ~y) & ~( ~x & y) );
}
/*
* isNotEqual - return 0 if x == y, and 1 otherwise
* Examples: isNotEqual(5,5) = 0, isNotEqual(4,5) = 1
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 6
* Rating: 2
*/
int isNotEqual(int x, int y) {
/*
* 2의 보수를 취해서 sign을 바꾸고 원래값을 더하면 같다면 0 다르면 1
*/
return !!(~y + 1 + x);
}
/*
* getByte - Extract byte n from word x
* Bytes numbered from 0 (LSB) to 3 (MSB)
* Examples: getByte(0x12345678,1) = 0x56
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 6
* Rating: 2
*/
int getByte(int x, int n) {
/*
* 추출할 바이트가 있는 위치로 시프트
* n * 8 은 n << 3 으로 대치
* 0xFF 로 AND 연산해서 1 바이트만 추출
*/
return ( x >> (n << 3) ) & 0xFF;
}
/*
* copyLSB - set all bits of result to least significant bit of x
* Example: copyLSB(5) = 0xFFFFFFFF, copyLSB(6) = 0x00000000
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 5
* Rating: 2
*/
int copyLSB(int x) {
/*
* 먼저 마지막 비트를 하나 뽑아내고
* 2의 보수를 취하면 0 인 경우 0, 1인 경우 -1 (0xFFFFFFFF)
*/
x = x & 1;
return ~x + 1;
}
/*
* logicalShift - shift x to the right by n, using a logical shift
* Can assume that 1 <= n <= 31
* Examples: logicalShift(0x87654321,4) = 0x08765432
* Legal ops: ~ & ^ | + << >>
* Max ops: 16
* Rating: 3
*/
int logicalShift(int x, int n) {
/*
* 시프트를 수행하고 유효부분 바깥 부분은 마스크 씌워서 제거
*/
return (~((1 << 31) >> (n + ~0))) & (x >> n);
}
/*
* 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) {
/*
* ABCDEFGH
* (A+B)+(C+D)+(E+F)+(G+H)
* (A+B+C+D)+(E+F+G+H)
* (A+B+C+D+E+F+G+H)
*/
int x1,x2,a;
a=0x55;
a=(a<<8)|a;
a=(a<<16)|a;
x1 = (x>>1)&a;
x2 = x&a;
x = x1+x2;
a=0x33;
a=(a<<8)|a;
a=(a<<16)|a;
x1 = (x>>2)&a;
x2 = x&a;
x = x1+x2;
a=0x0F;
a=(a<<8)|a;
a=(a<<16)|a;
x1 = (x>>4)&a;
x2 = x&a;
x = x1+x2;
a=0xFF;
a=a|(a<<16);
x1 = (x>>8)&a;
x2 = x&a;
x = x1+x2;
a=0xFF;
a=a|(a<<8);
x1 = (x>>16)&a;
x2 = x&a;
x = x1+x2;
return x;
}
/*
* bang - Compute !x without using !
* Examples: bang(3) = 0, bang(0) = 1
* Legal ops: ~ & ^ | + << >>
* Max ops: 12
* Rating: 4
*/
int bang(int x) {
/*
* 절반씩 OR 중첩해가면서 1이 하나라도 존재하는지 체크하고
* 마지막에 전체 비트를 뒤집어 마지막 비트만 리턴
*/
x = (x >> 16) | x;
x = (x >> 8) | x;
x = (x >> 4) | x;
x = (x >> 2) | x;
x = (x >> 1) | x ;
x = ~x & 1;
return x;
}
/*
* leastBitPos - return a mask that marks the position of the
* least significant 1 bit. If x == 0, return 0
* Example: leastBitPos(96) = 0x20
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 6
* Rating: 4
*/
int leastBitPos(int x) {
/*
* 전체 비트를 뒤집어서 교차시키면서 마지막 1 비트만 추출한다.
*/
return ((~x+1)^(~x)) & x;
}
/*
* TMax - return maximum two's complement integer
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 4
* Rating: 1
*/
int tmax(void) {
/*
* 1000000000000000 을 뒤집어서 양의 최대값을 만들어낸다.
*/
int x = 1;
return ~(x << 31);
}
/*
* 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) {
/*
* MSB를 추출하여 체크한다.
*/
return !(~(~x >> 31));
}
/*
* isGreater - if x > y then return 1, else return 0
* Example: isGreater(4,5) = 0, isGreater(5,4) = 1
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 24
* Rating: 3
*/
int isGreater(int x, int y) {
/*
* x y r
* 1 0 0
* 0 1 1
* 0 0 ?
* 1 1 ?
* x y MSB가 다르면 y의 MSB가 답이고
* x y MSB가 같으면 차를 계산한것의 MSB가 답이다.
*/
return !!((((~(x^y))&(y+~x+1))|(y&(x^y)))>>31);
}
/*
* divpwr2 - Compute x/(2^n), for 0 <= n <= 30
* Round toward zero
* Examples: divpwr2(15,1) = 7, divpwr2(-33,4) = -2
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 15
* Rating: 2
*/
int divpwr2(int x, int n) {
/*
* 0이나 양수인 경우는 시프트하고 값을 버려도 되지만,
* 음수인 경우는 버려질 n비트가 0이 아니라면 1을 더해줘야한다.
*/
int msb;
int round;
msb = !!((1 << 31) & x);
round = !!((~((~0) << n) & x));
return (x >> n) + (msb & round);
}
/*
* abs - absolute value of x (except returns TMin for TMin)
* Example: abs(-1) = 1.
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 10
* Rating: 4
*/
int abs(int x) {
/*
* (+) 연산자 앞부분 : MSB에 따라 다른 마스크를 생성
* (+) 연산자 뒷부분 : MSB에 따라 더할 값을 결정
* 음수일 때는 XOR에 의해 1의 보수가 만들어지고 1을 더해서 부호를 변환한다.
*/
return (x^(x >> 31))+(!!(x & (1 << 31)));
}
/*
* 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) {
/*
* x 와 y가 같은 부호일 때만 오버플로우 가능성이 존재한다.
* x + y 값이 x, y 가 같은 부호인데 x나 y와 다른 부호이면 오버플로우로 판정한다.
*/
int a, b, s;
int mask;
mask = 0x80 << 24;
s = x+y;
a = !(((x & mask) ^ (y & mask)) & mask);
b = !!(((x & mask) ^ (s & mask)) & mask);
return !(a&b);
}
}