93.6k views
1 vote
Float_abs - return bit-level equivalent of absolute value of f for * floating point argument f. * both the argument and result are passed as unsigned int's, but * they are to be interpreted as the bit-level representations of * single-precision floating point values. * when argument is nan, return argument.. * legal ops: any integer/unsigned operations incl. ||, &&. also if, while * max ops: 10 * rating: 2

User Lindel
by
8.5k points

2 Answers

5 votes

Final answer:

In computer science, finding the bit-level absolute value of a floating-point number involves identifying and possibly changing the sign bit using bitwise operations. If the number is NaN, it is returned unchanged. The key operations involve the use of masks and bitwise AND.

Step-by-step explanation:

The student's question is related to Computer Science, specifically the topic of floating-point numbers and their representation in binary form. When working with floating-point arithmetic at the bit level, the goal is to find the absolute value of a given floating-point number represented as an unsigned int. This involves manipulating the bits of the floating-point number such that, if the number is not a NaN (Not a Number), only the sign bit of the number is changed (if necessary) to ensure the value is non-negative.

To accomplish this using bit-level operations:

  1. First, identify the sign bit of the floating-point representation. In IEEE 754 format for single-precision numbers, this is the most significant bit.
  2. Next, use bitwise operations to clear the sign bit, effectively taking the absolute value.
  3. If the original number was NaN, ensure that the outcome does not change as NaN has no sign.

The operation to clear the sign bit typically involves using a mask with all bits set to 1 except the sign bit. By using a bitwise AND operation with this mask, the result will have the sign bit cleared without modifying any other bits of the representation.

User Zeusstl
by
8.2k points
4 votes

#include <stdio.h>

int main(void) {

// your code goes here

//unsigned a =float_times_four(0x80000000);

unsigned float_times_four(unsigned uf){

unsigned expn = (uf >> 23) & 0xFF;

printf(expn);

unsigned sign = uf & 0x80000000;

unsigned frac = uf & 0x007FFFFF;

if(expn == 255 ||(expn == 0 && frac ==0))

return uf;

if(expn){

expn<<2;

}else if(frac == 0x007FFFFF){

//here 0x7FFFFF given by you that is wrong you place this 0x007FFFFF will excute

frac>>2;

expn<<2;

}else{

frac<<=2;

}

return (sign) | (expn <<23) | (frac);

}

return 0;

}

User CpoDesign
by
8.5k points