C++位操作练习

Puzzles

The following table describes the 10 puzzles that you will be solving in bits.c. The ``Rating'' field gives the difficulty rating (the number of points) for the puzzle, and the ``Max ops'' field gives the maximum number of operators you are allowed to use to implement each function.

NameDescriptionRatingMax Ops
bitAnd(x,y)Returns (x & y) using only ~ and |18
bitOr(x,y)Returns (x | y) using only ~ and &18
isZero(x)Returns 1 if x == 0 and 0 otherwise12
minusOne()Returns a value of -112
tmax()Returns the maximum two's complement integer14
bitXor(x, y)Returns (x ^ y) using only ~ and &214
getByte(x)Extract byte number n from word x214
isEqual(x,y)Returns 1 if x == y, and 0 otherwise25
negate(x)Returns -x25
isPositive(x)Returns 1 if x > 0, and 0 otherwise38
  • Function bitAnd computes the And function. That is, when applied to arguments x and y, it returns (x & y). You may only use the operators ~ and |.
  • Similarly, function bitOr computes the Or function. That is, when applied to arguments x and y, it returns (x | y). You may only use the operators ~ and &.
  • A predicate is a function that returns either true or false. Function isZero is a predicate that returns a value of 1 (true) if x is equal to zero. Otherwise, it returns a value of 0 (false).
  • Function minusOne takes no arguments and always returns a value of -1, without using the minus operator.
  • Function tmax, which also takes no arguments, returns the largest positive two's complement number.
  • Function bitXor should duplicate the behavior of the bitwise Xor operation ^, using only the operations & and ~.
  • Function getByte extracts a byte from a word and returns that byte. The bytes within a word are ordered from 0 (least significant) to 3 (most significant). For example, getByte(0x12345678,1) = 0x56
  • Function isEqual compares x to y for equality, returning 1 (true) if x == y and 0 (false) otherwise.
  • Function negate computes and returns the negative of its input argument x, without using the minus operator.
  • Function isPositive is a predicate that returns 1 (true) if its input argument is greater than zero. Otherwise, it returns 0 (false).

代码,要求,及我的答案和注释

 

/* 
 * bits.c - Source file with your solutions to the Lab.
 *          This is the file you will hand in to your instructor.
 */

#include "btest.h"
#include <limits.h>

/*
 * Instructions to Students:
 *
 * STEP 1: Fill in the following struct with your identifying info.
 */
info_struct info =
{
   
/* Replace with your full name */
   
"full name",
   
/* Replace with your login ID */
   
"login ID",
};

#if 0
/*
 * STEP 2: Read the following instructions carefully.
 */

You will provide your solution to the Data Lab 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 functionYour code 
  must conform to the following style
:
 
  
int Funct(arg1arg2, ...) {
      
/* 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 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 operatorsYou are not restricted to
  one 
operator per line.

  
You are expressly forbidden to:
  
1. Use any control constructs such as ifdowhileforswitchetc.
  
2. Define or use any macros.
  
3. Define any additional functions in this file.
  
4. Call any functions.
  
5. Use any other operationssuch as &&, ||, -, ?, or []:
  
6. Use any form of casting.
 
  
You may assume that your machine:
  
1. Uses 2s complement32-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 (<< 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 = (<< x);
     
result += 4;
     
return result;
  }


NOTES AND HINTS:
  
1. Each function has a maximum number of operators (! ~ & ^ | + << >>)
     
that you are allowed to use for your implementation of the function
     
The max operator count will be checked by your instructor
     
Note that '=' is not countedyou may use as many of these as you 
     want without penalty
.
  
2. Use the btest test harness to check your functions for correctness.
#endif

/*
 * STEP 3: Modify the following functions according the coding rules.
 */



/* 
 * bitAnd - x&y using only ~ and | 
 *   Example: bitAnd(6, 5) = 4
 *   Legal ops: ~ |
 *   Max ops: 8
 *   Rating: 1
 */
int bitAnd(int xint y) {
/* 
 * If the result of ~x|~y is 0, it means x and y are both 1 at corresponding bit, 
 * only both 1 make the bit 1,  other situations will make 1. This is just opposite to
 * bitAnd logic. So we got ~(~x|~y)
 */
  
return ~(~x|~y);
}

/* 
 * bitOr - x|y using only ~ and & 
 *   Example: bitOr(6, 5) = 7
 *   Legal ops: ~ &
 *   Max ops: 8
 *   Rating: 1
 */
int bitOr(int xint y) {
/* 
 * Only 0|0 makes 0, others make 1. So what we are going to find is both 0 at corresponding bit.
 * Only 1&1 makes 1, so only if both x and y are 0 the result of ~x&~y is 1,this will find out
 * both 0. But it is just opposite to bitOr, so we got ~(~x&~y)
 */
  
return ~(~x&~y);
}

/*
 * isZero - returns 1 if x == 0, and 0 otherwise 
 *   Examples: isZero(5) = 0, isZero(0) = 1
 *   Legal ops: ! ~ & ^ | + << >>
 *   Max ops: 2
 *   Rating: 1
 */
int isZero(int x) {
/* When x is zero , !x is 1. When x is not zero, !x is 0 */
  
return !x;
}

/* 
 * minusOne - return a value of -1 
 *   Legal ops: ! ~ & ^ | + << >>
 *   Max ops: 2
 *   Rating: 1
 */
int minusOne(void) {
/* In memory, -1 is represented as all 1 at every bit, so we got ~0*/
  
return ~0;
}

/* 
 * TMax - return maximum two's complement integer 
 *   Legal ops: ! ~ & ^ | + << >>
 *   Max ops: 4
 *   Rating: 1
 */
int tmax(void) {
/* 
 * The largest positive integer is 01111111111111111111111111111111,
 * it's two's complement is  1000000000000000000000000000000
 */
  
return ~(1<<31);
}

/* 
 * bitXor - x^y using only ~ and & 
 *   Example: bitXor(4, 5) = 1
 *   Legal ops: ~ &
 *   Max ops: 14
 *   Rating: 2
 */
int bitXor(int xint y) {
/* 
 *   Use x&y to find both 1 and ~x&~y to find both 0,the result of them at corresponding bit
 *   will be 1. ~(result1|result2) is the final result we want. Bring them into the bitOr above,
 *   we got ~(x&y)&~(~x&~y)
 */
   
return ~(x&y)&~(~x&~y);
}

/* 
 * 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 xint n) {
/* Right shift x and makes higher part zero. n<<3 bits to shift  */
   
return (x>>(n<<3))&(0x000000FF);
}

/* 
 * isEqual - return 1 if x == y, and 0 otherwise 
 *   Examples: isEqual(5,5) = 1, isEqual(4,5) = 0
 *   Legal ops: ! ~ & ^ | + << >>
 *   Max ops: 5
 *   Rating: 2
 */
int isEqual(int xint y) {
/* If x is equal to y , every bit of x^y will be 0, !0 will be 1  */
  
return !(x^y);
}

/* 
 * negate - return -x 
 *   Example: negate(1) = -1.
 *   Legal ops: ! ~ & ^ | + << >>
 *   Max ops: 5
 *   Rating: 2
 */
int negate(int x) {
/* This is just the way how to transform a positive to a nagative */ 
  
return ~x+1;
}

/* 
 * isPositive - return 1 if x > 0, return 0 otherwise 
 *   Example: isPositive(-1) = 0.
 *   Legal ops: ! ~ & ^ | + << >>
 *   Max ops: 8
 *   Rating: 3
 */
int isPositive(int x) {
/* 
 *   Use x>>31 to get the sign bit, when x is positve ~(x>>31) is 1, else it's 0.
 *   !x to verify whether x is 0, if it's 0, !x will be 1, and !!x will be 0.
 *   So if x is zero, it will return 0, and if x is not zero, it will return the first part.
 *   That's what we want
 */
  
  
return ~(x>>31)&(!!x);
}
  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值