티스토리 뷰
/*
* bitAnd - x&y using only ~ and |
* Example: bitAnd(6, 5) = 4
* Legal ops: ~ |
* Max ops: 8
* Rating: 1
*/
int bitAnd(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) {
return (~(~x&~y))&(~(x&y));
}
/*
* evenBits - return word with all even-numbered bits set to 1
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 8
* Rating: 2
*/
int evenBits(void) {
int i = 0x55;
return (i<<24)|(i<<16)|(i<<8)|i;
}
/*
* 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) {
return (x<<31)>>31;
}
/*
* isLessOrEqual - if x <= y then return 1, else return 0
* Example: isLessOrEqual(4,5) = 1.
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 24
* Rating: 3
*/
int isLessOrEqual(int x, int y) {
int x_neg = x>>31;
int y_neg = y>>31;
return ((x_neg & !y_neg) | (!(x_neg ^ y_neg) & (~y+x)>>31));
}
/*
* logicalNeg - implement the ! operator, using all of
* the legal operators except !
* Examples: logicalNeg(3) = 0, logicalNeg(0) = 1
* Legal ops: ~ & ^ | + << >>
* Max ops: 12
* Rating: 4
*/
int logicalNeg(int x) {
return ((~(~x+1)&(~x))>>31)&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 (x >> n) & ((1 << (32 + (~n+1))) + ~0);
}
/*
* sum3 - x+y+z using only a single '+'
* Example: sum3(3, 4, 5) = 12
* Legal ops: ! ~ & ^ | << >>
* Max ops: 16
* Rating: 3
*/
/* A helper routine to perform the addition. Don't change this code */
static int sum(int x, int y) {
return x+y;
}
int sum3(int x, int y, int z) {
int word1 = sum(x,y);
int word2 = z;
return sum(word1,word2);
}
'Study' 카테고리의 다른 글
AWS(Amazon Web Service, 아마존웹서비스) CLI(Command Line Interface, 씨엘아이) Install(설치) 관련 (0) | 2024.01.27 |
---|---|
VBScript(브이비스크립트) Key Excute(키 실행) (0) | 2024.01.27 |
JSP(제이에스피) 조건문 (0) | 2024.01.27 |
Postman(포스트맨) 관련 (0) | 2024.01.27 |
ping port(핑 포트) (1) | 2024.01.27 |