Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8655267
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T14:59:09+00:00 2026-06-12T14:59:09+00:00

How can I convert an integer to a half precision float (which is to

  • 0

How can I convert an integer to a half precision float (which is to be stored into an array unsigned char[2]). The range to the input int will be from 1-65535. Precision is really not a concern.

I am doing something similar for converting to 16bit int into an unsigned char[2], but I understand there is not half precision float C++ datatype. Example of this below:

int16_t position16int = (int16_t)data;
memcpy(&dataArray, &position16int, 2);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T14:59:10+00:00Added an answer on June 12, 2026 at 2:59 pm

    It’s a very straightforward thing, all the info you need is in Wikipedia.

    Sample implementation:

    #include <stdio.h>
    
    unsigned int2hfloat(int x)
    {
      unsigned sign = x < 0;
      unsigned absx = ((unsigned)x ^ -sign) + sign; // safe abs(x)
      unsigned tmp = absx, manbits = 0;
      int exp = 0, truncated = 0;
    
      // calculate the number of bits needed for the mantissa
      while (tmp)
      {
        tmp >>= 1;
        manbits++;
      }
    
      // half-precision floats have 11 bits in the mantissa.
      // truncate the excess or insert the lacking 0s until there are 11.
      if (manbits)
      {
        exp = 10; // exp bias because 1.0 is at bit position 10
        while (manbits > 11)
        {
          truncated |= absx & 1;
          absx >>= 1;
          manbits--;
          exp++;
        }
        while (manbits < 11)
        {
          absx <<= 1;
          manbits++;
          exp--;
        }
      }
    
      if (exp + truncated > 15)
      {
        // absx was too big, force it to +/- infinity
        exp = 31; // special infinity value
        absx = 0;
      }
      else if (manbits)
      {
        // normal case, absx > 0
        exp += 15; // bias the exponent
      }
    
      return (sign << 15) | ((unsigned)exp << 10) | (absx & ((1u<<10)-1));
    }
    
    int main(void)
    {
      printf(" 0: 0x%04X\n", int2hfloat(0));
      printf("-1: 0x%04X\n", int2hfloat(-1));
      printf("+1: 0x%04X\n", int2hfloat(+1));
      printf("-2: 0x%04X\n", int2hfloat(-2));
      printf("+2: 0x%04X\n", int2hfloat(+2));
      printf("-3: 0x%04X\n", int2hfloat(-3));
      printf("+3: 0x%04X\n", int2hfloat(+3));
      printf("-2047: 0x%04X\n", int2hfloat(-2047));
      printf("+2047: 0x%04X\n", int2hfloat(+2047));
      printf("-2048: 0x%04X\n", int2hfloat(-2048));
      printf("+2048: 0x%04X\n", int2hfloat(+2048));
      printf("-2049: 0x%04X\n", int2hfloat(-2049)); // first inexact integer
      printf("+2049: 0x%04X\n", int2hfloat(+2049));
      printf("-2050: 0x%04X\n", int2hfloat(-2050));
      printf("+2050: 0x%04X\n", int2hfloat(+2050));
      printf("-32752: 0x%04X\n", int2hfloat(-32752));
      printf("+32752: 0x%04X\n", int2hfloat(+32752));
      printf("-32768: 0x%04X\n", int2hfloat(-32768));
      printf("+32768: 0x%04X\n", int2hfloat(+32768));
      printf("-65504: 0x%04X\n", int2hfloat(-65504)); // legal maximum
      printf("+65504: 0x%04X\n", int2hfloat(+65504));
      printf("-65505: 0x%04X\n", int2hfloat(-65505)); // infinity from here on
      printf("+65505: 0x%04X\n", int2hfloat(+65505));
      printf("-65535: 0x%04X\n", int2hfloat(-65535));
      printf("+65535: 0x%04X\n", int2hfloat(+65535));
      return 0;
    }
    

    Output (ideone):

     0: 0x0000
    -1: 0xBC00
    +1: 0x3C00
    -2: 0xC000
    +2: 0x4000
    -3: 0xC200
    +3: 0x4200
    -2047: 0xE7FF
    +2047: 0x67FF
    -2048: 0xE800
    +2048: 0x6800
    -2049: 0xE800
    +2049: 0x6800
    -2050: 0xE801
    +2050: 0x6801
    -32752: 0xF7FF
    +32752: 0x77FF
    -32768: 0xF800
    +32768: 0x7800
    -65504: 0xFBFF
    +65504: 0x7BFF
    -65505: 0xFC00
    +65505: 0x7C00
    -65535: 0xFC00
    +65535: 0x7C00
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I convert the char array x to an integer 89 in the
In Java, we can convert an int to float implicitly, which may result in
In MSSQL you can convert a string into an integer like this: CONVERT(INT, table.column)
The exercise asks for a code which can convert the user input of the
Possible Duplicate: Can't convert String into integer in ruby/ruby-on-rails I'm running through a tutorial
I can convert an integer into string using String s = + 4; //
I can use Convert.ToString to convert an integer to its binary representation: int x
How can I convert a List<Integer> to int[] in Java? I'm confused because List.toArray()
I am getting the following error: Can't convert String into Integer (TypeError) It is
Here is my simple question We can convert integer, float, double to String like

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.