I am currently trying to separate the different parts of single precision floating point from IEEE 754 using C bitwise operators. I plan to put the separated parts in a struct. My end goal is to write arithmetic operations using bitwise operators.
I have however stumbled upon a little issue where my results don't make any sense whatsoever. I have been unable to find a solution to this problem and have been unable to find a solution on the internet. Any insight in this would be greatly appreciated.
The following is all the modules I've used.
//test.c
#include <stdio.h>
#include "splicing.h"
int main(void)
{
float a = 5, b = -3, c = 0.1;
sploat A, B, C;
printf("%f\n%x\n", a, *(unsigned int*) &a);
printf("%f\n%x\n", b, *(unsigned int*) &b);
printf("%f\n%x\n\n", c, *(unsigned int*) &c);
splice(a, A);
splice(b, B);
splice(c, C);
printf("%f\n%hhu %hhi %x\n\n", a, A.s, A.e, A.m);
printf("%f\n%hhu %hhi %x\n\n", b, B.s, B.e, B.m);
printf("%f\n%hhu %hhi %x\n\n", c, C.s, C.e, C.m);
return 0;
}
/*
* Expected results
*
* 5 = 0x40a00000
* exp = +2
* man = 0x200000 (explicit) 0xa00000 (spliced)
* sign = 0
*
* -3 = 0xc0400000
* exp = +1
* man = 0x400000 (explicit) 0xc00000 (spliced)
* sign = 1
*
* 0.1 = 0x3dccccd
* exp = -4
* man = 0x4ccccc (explicit) 0xcccccc (spliced)
* sign = 0
*/
//splicing.h
typedef struct splicedflt{
unsigned char s; //sign
signed char e; //exponent
unsigned int m; //mantissa
} sploat; //short for spliced float
//unfinished
//Makes inserted sploat reflect inserted float. The problem child I need help with.
int splice(float, sploat);
//splicing.c
int splice(float num, sploat strukt)
{
unsigned int raw = *(unsigned int*) # //floats don't allow for bitmagic.
strukt.s = raw >> 31;
strukt.e = (raw << 1) >> 24;
strukt.m = ((raw << 9) >> 9) | 0x1000000;
return 0;
}
The following is output from the program. I have no idea why this is not working.
$ gcc test.c
$ ./a.out
5.000000
40a00000
-3.000000
c0400000
0.100000
3dcccccd
5.000000
0 0 0
-3.000000
160 0 5588
0.100000
160 -20 7ffe
$