Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a \'c\' code for the following puzzles, Bit Manipulations Table 1 describe

ID: 3782788 • Letter: W

Question

Write a 'c' code for the following puzzles,

Bit Manipulations Table 1 describes a set of functions that manipulate and test sets of bits. The “Points” field gives 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. See the comments in bitslab.c for more details on the desired behavior of the functions.

Name Description Points Max Ops

bitAnd(x,y) x & y using only | and ˜ 2 8

getByte(x,n) Get byte n from x. 2 6

logicalShift(x,n) Shift right logical. 3 20

bitCount(x) Count the number of 1’s in x. 4 40

bang(x) Compute !x without using ! operator. 4 12

Two’s Complement Arithmetic Table 2 describes a set of functions that make use of the two’s complement representation of integers. Again, refer to the comments in bitslab.c for more information.

Name Description Points Max Ops

tmin() Most negative two’s complement integer 1 4

fitsBits(x,n) Does x fit in n bits? 2 15

divpwr2(x,n) Compute x/2 n 2 15

negate(x) -x without negation 2 5

isPositive(x) x > 0? 3 8

isLessOrEqual(x,y) x <= y? 3 24

Floating-Point Operations For this part of the assignment, you will implement some common single-precision floating-point operations. In this section, you are allowed to use standard control structures (conditionals, loops), and you may use both int and unsigned data types, including arbitrary unsigned and integer constants. You may not use any unions, structs, or arrays. Most significantly, you may not use any floating point data types, operations, or constants. Instead, any floating-point operand will be passed to the function as having type unsigned, and any returned floating-point value will be of type unsigned. Your code should perform the bit manipulations that implement the specified floating point operations. Table 3 describes a set of functions that operate on the bit-level representations of floating-point numbers. Refer to the comments in bits.c for more information.

Name Description Points Max Ops

float_neg(uf) Compute -f 4 10

float_i2f(x) Compute (float) x 4 30

float_twice(uf) Computer 2*f 4 30

Explanation / Answer

#include<stdio.h>


int bitAnd(int x,int y)
{
   return ~(~x | ~y);
}
int getByte(int x,int n)
{
   return (x >> (8*n)) & 0xff;
}

int logicalShift(int x,int n)

{
   /* shift the word arithmatically, then use maskbits to clear the left-end bytes */
int maskbits = (1<<31)>>n; //shift only 31 to prevent overflow
maskbits = maskbits<<1;
maskbits=~maskbits;
x=x>>n;

return x&maskbits;
}

int bitCount(int x)
{
   unsigned int count = 0;
while(x)
{
count += x & 1;
x >>= 1;
}
return count;
}

int bang(int x)
{
   int in = ~x;

int neg = in + 1;

return ((~neg & in)>>31) & 1;

}

int fitsBits(int x, int n) {
int r, c;
c = 33 + ~n;
r = !(((x << c)>>c)^x);
return r;
}

int divpwr2(int x,int n)
{
   return (x + ((x >> 31) & ((1 << n) + ~0))) >> n;
}

int negate(int x)
{
   int f;
f = ~x;
f = f+1;
return f;
}
int tmin()
{
return 1 << 31; /*shift the '1' bit to get 0x80000000*/
}

int isPositive(int x)
{
   return !((x & 0x80000000) >> 31 | !x);
}
int isLessOrEqual(int x, int y)
{
int negX = ~x+1;
int addY = negX + y; /*negative if x > y*/
int checkSign = addY >> 31 & 1; /*shifts sign bit to the right*/

  
int leftBit = 1 << 31;
int xLeft = leftBit & x;
int yLeft = leftBit & y;
int xOrd = xLeft ^ yLeft;
xOrd = (xOrd >> 31) & 1;

return (xOrd & (xLeft>>31)) | (!checkSign & !xOrd);
}
unsigned float_neg(unsigned uf)
{
int nan = 0x000000FF << 23; /*1's in the 8 exponent bits*/
int fractiontion = 0x7FFFFF & uf;
/*return argument if exp bits are all 1's and fractiontion is not zero*/
if((nan & uf) == nan && fractiontion)
return uf;

/* just flip the sign bit*/
return uf ^ (1 << 31);
}
unsigned float_twice(unsigned uf) {
/* if its denormalized double fractiontiontion, if its normailized, increase
* exponent, if it on the edege, decrement fractiontiontion, increment epx. */
unsigned ex = (uf >> 23) & 0xFF;
unsigned sign = uf & 0x80000000;
unsigned fractiontion = uf & 0x007FFFFF;
if (ex == 255 || (ex == 0 && fractiontion == 0))
return uf;

if (ex) {
ex++;
} else if (fractiontion == 0x7FFFFF) {
fractiontion--;
ex++;
} else {
fractiontion <<= 1;
}

return (sign) | (ex << 23) | (fractiontion);
}
unsigned float_i2f(int x) {
int e = 158;
int maskbits = 1<<31;
int sign = x&maskbits;
int fraction;
if (x == maskbits)
return maskbits | (158<<23);
if (!x)
return 0;
if (sign)
x = ~x + 1;
while (!(x&maskbits)) {
x = x<<1;
e = e -1;
}
fraction = (x&(~maskbits)) >> 8;
if (x&0x80 && ((x&0x7F) > 0 || fraction&1))
fraction = fraction + 1;
return sign + (e<<23) + fraction;
}
int main(int argc, char const *argv[])
{
   printf("============Assignment 1==================== ");
   int x=1000;
   int y=10;
  
   printf("%d ",bitAnd(x,y));
   printf("%d ",getByte(x,y));
   printf("%d ",logicalShift(x,y));
   printf("%d ",bitCount(x));
   printf("%d ",bang(x));
  
   printf("============Assignment 2==================== ");
   printf("%d ",tmin() );
   printf("%d ",fitsBits(x,y));
   printf("%d ",divpwr2(x,y));
   printf("%d ",negate(x));
   printf("%d ",isPositive(x));
   printf("%d ",isLessOrEqual(x,y));
   printf("============Assignment 3==================== ");
   printf("%d ",float_neg(x));
   printf("%d ",float_twice(x) );
       printf("%d ",float_i2f(x) );

   return 0;
}

===================================================

Output:


akshay@akshay-Inspiron-3537:~/Chegg$ ./a.out
============Assignment 1====================
8
0
0
6
0
============Assignment 2====================
-2147483648
0
0
-1000
1
0
============Assignment 3====================
-2147482648
2000
1148846080

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote