I want to create a 16-bit float datatype, allocating memory to exponential and mantissa part at my own discretion. How can I do that?

## All 4 Replies

Why do you want to do that?
you can use structures
say

``````struct myFloat{
short int sign:1;
short int expo:6;
short int mant:11;

float toFloat(){
// conversion
}

// overloaded arithmetic operations

// constructors and initializations
}``````

hope this helps:)

I want to create a 16-bit float datatype, allocating memory to exponential and mantissa part at my own discretion. How can I do that?

Do you want a portable solution?
No: use short int (if it occupies exactly 16 bits on the target platform)...
Yes: use `class TinyFloat {... unsigned char rep; ...};` ...
Both cases: use bitwise operators <<, >>, &, | to pack and unpack bit fields.

Can u plz explain in a lil more detail.... I m new to bit-fields and such stuff.

Do you want a portable solution?
No: use short int (if it occupies exactly 16 bits on the target platform)...
Yes: use `class TinyFloat {... unsigned char rep; ...};` ...
Both cases: use bitwise operators <<, >>, &, | to pack and unpack bit fields.

Of course, you are new in bit fields and other stuff (see OP ;))
http://www.cprogramming.com/tutorial/bitwise_operators.html
http://www.learncpp.com/cpp-tutorial/38-bitwise-operators/
For example, let rep is a mantissa and rep represents:

``````bits 0..5 - binary exponent
bit6 - exponent sign
bit7 - number sign``````
``````(rep&0x3F) // get exponent
(rep&040)  // get exponent sign
(rep&0x80) // get number sign
(negative?rep|0x80:rep&~0x80) // set negative
rep &= ~0F0 // abs(x)``````

and so on.