How to quadruple an unsigned number using bit-wise and logic operator in C

时间:2016-02-03 04:05:23

标签: c floating-point precision unsigned

Goal:

4x ( 4.400000095 ) = 17.60000038
  • Legal ops: Any integer/unsigned operations incl. ||, &&. also if, while
  • Max ops: 30
  • Return bit-level equivalent of expression x + x + x + x for
  • floating point argument f.

My code:

unsigned 4x(unsigned uf) {
unsigned expn = (uf >> 23) & 0xFF;
unsigned sign = uf & 0x80000000;
unsigned frac = uf & 0x007FFFFF;
if (expn == 255 || (expn == 0 && frac == 0))  
    return uf;
if (expn) {
    expn << 2;
} else if (frac == 0x7FFFFF) {
    frac >> 2;
    expn << 2;
} else {
    frac <<= 2;
}

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

}

As you can guess, my code does not work. Instead of quadrupling the input, the input is doubled. I don't know why since the fraction and exponent are always being right / left shifted by 2 instead of 1. Im working with single precision floating point values in 32 bit machines.

2 个答案:

答案 0 :(得分:3)

Note that

expn << 2;

does not modify expn. You probably want

expn <<= 2;

Ditto for

frac >> 2;
expn << 2;

However, as @chux pointed out, you only need to increase add 2 to the exponent, not multiply the exponent by 4.

答案 1 :(得分:1)

一些未经测试的代码 - 留给OP。 (GTG)

棘手的一点是处理* 4变为正常时的子正常数。还要注意溢出到无穷大的大值。如果您想忽略子法线,只需expn += 2并检查溢出。

对于正常数字,另一种方法expn += 2。对于子法线,请移动frac <<= 2并处理正常情况。

代码大约是30个操作。

#include <stdint.h>

float x4(float x) {
  // Use union to access the bits.  Leap-of-faith here (float is 32 bits, endian)
  union {
    float f;
    uint32_t u32;
  } u;
  u.f = x;
  uint32_t expn = (u.u32 >> 23) & 0xFF;
  uint32_t sign = u.u32 & 0x80000000;
  uint32_t frac = u.u32 & 0x007FFFFF;

  // Nan Inf
  if (expn == 255) return u.f;

  if (expn == 0) {
    expn++;  // Bring sub-normal into normal expo range
  } else {
    frac += 0x800000; // restore implied bit
  }

  // *4
  frac <<= 2;

  // normalize - this usually iterates twice, less for sub-normals
  while (frac > 0xFFFFFF) {
    expn++;
    frac >>= 1; // 1's will not be shifted out as 2 LSB are 0 so no later rounding
  }

  // overflow to inf
  if (expn >= 255) {
    expn = 255;
    frac = 0;
  } else if (frac & 0x800000) {
    frac ^= 0x800000; // clear implied bit
  } else {
    // still sub-normal
    expn--;  // should now be 0
  }

  u.u32 = sign | (expn << 23) | frac;
  return u.f;   
}