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?

Recommended Answers

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[2]; ...}; ...
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[2]; ...}; ...
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[0] is a mantissa and rep[1] represents:

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

and so on.
See also operator overloading in C++.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.