• Howdy! Welcome to our community of more than 130.000 members devoted to web hosting. This is a great place to get special offers from web hosts and post your own requests or ads. To start posting sign up here. Cheers! /Peo, FreeWebSpace.net
managed wordpress hosting

Biggest C newbie ever. Please help ... sinking in bitwise quicksand.

Biloela

New Member
HELP! Finally learning C. Understand the concepts but the code is hurting. Can anyone help me with my missing functions?

Thanks in advance.



******************************************************************************


You are expressly FORBIDDEN (except for the multiply function at the end) 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.

YOU SHALL REPLACE 'return 0;' at the end of each function you need to complete
WITH YOUR SOLUTION IMPLEMENTATION (e.g., return ~x & ~y; in bitNor)

******************************************************************************/


/*
* bitNor - ~(x|y) using only ~ and &
* Example: bitNor(0x6, 0x5) = 0xFFFFFFF8
* Legal operators: ~ &
*/
int bitNor(int x, int y) {
/* DeMorgan's law to the rescue */
return ~x & ~y;
}





/*
* bitXor - x^y using only ~ and &
* Example: bitXor(4, 5) = 1
* Legal operators: ~ &
*
* #marks: 1
*/
int bitXor(int x, int y) {
/* The definition of XOR is (~x & y) | (x & ~y).
* The use DeMorgan's law ( ~(~a & ~b) a few times
* to get the appropriate result.
*/
return ~((~(~x & y)) & (~(x & ~y)));
}




/*
* isNotEqual - return 0 if x == y, and 1 otherwise
* Examples: isNotEqual(5,5) = 0, isNotEqual(4,5) = 1
* Legal operators: ! ~ & ^ | + << >>
*
* #marks: 1
*/
int isNotEqual(int x, int y) {
/* uses fact that if x and y are the same,
then the exclusive or (XOR) of x and y (x ^ y) is 0 */
return (!!(x ^ y));
}






/*
* getByte - Extract byte n from word x
* Bytes numbered from 0 (LSB) to 3 (MSB)
* Examples: getByte(0x12345678,1) = 0x56
* Legal ops: ! ~ & ^ | + << >>
*
* #marks: 2
*/
int getByte(int x, int n) {
/* shift the wanted byte to be least significant and mask out any more significant */
return (0xff & (x >> (n << 3)));
}






/*
* copyLSB - set all bits of result to least significant bit of x
* Example: copyLSB(5) = 0xFFFFFFFF, copyLSB(6) = 0x00000000
* Legal operators: ! ~ & ^ | + << >>
*
* #marks: 1
*/
int copyLSB(int x) {
/* Shift LSB to be MSB, and then exploit arithmetic right shift to fill all bits with it */
return ((x << 31) >> 31);
}






/*
* logicalShift - shift x to the right by n, using a logical shift
* Can assume that 1 <= n <= 31
* Examples: logicalShift(0x876543210,4) = 0x087654321
* Legal operators: ~ & ^ | + << >>
*/
int logicalShift(int x, int y) {
/* Shift normally, using XOR to clear any bits on the left of the result that should always be 0 instead of the sign */
return (x >> y) ^ (((x & (1 << 31)) >> y) << 1);
}





/*
* leastBitPos - return a mask that marks the position of the
* least significant 1 bit. If x == 0, return 0
* Example: leastBitPos(96) = 0x20
* Legal operators: ! ~ & ^ | + << >>
*
* #marks: 2
*/
int leastBitPos(int x) {
int negative_x = ~x + 1;
return (x & negative_x);
}





/*
* isNonNegative - return 1 if x >= 0, return 0 otherwise
* Example: isNonNegative(-1) = 0. isNonNegative(0) = 1.
* Legal operators: ! ~ & ^ | + << >>
*
* #marks: 1
*/
int isNonNegative(int x) {
/* logical negation of sign bit in all positions */
return ((~x) >> 31) & 0x1;
}





/*
* abs - absolute value of x
* Example: abs(-1) = 1. abs(32) = 32. abs(-210) = 210.
* Legal ops: ! ~ & ^ | + << >>
*
* #marks: 2
*/
int abs(int x) {
/* Get a mask of all the sign bit, XOR with it to flip all bits
if x < 0, and then add 1 if x < 0
REMEMBER: you can assume your machine performs right shifts arithmetically
*/
int sign = x >> 31;
return 0;
}





/*
* multiply - return the product of x and y
* Example: multiply(3, 5) = 15.
* Legal operators: any thing except '*'; must use some bitwise operators
*
* #marks: 5
*/
unsigned long multiply(unsigned int x, unsigned int y) {









return 0;
}
 
Back
Top