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 7616825
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T02:59:06+00:00 2026-05-31T02:59:06+00:00

Say I have a float that I want to interpret as an unsigned int

  • 0

Say I have a float that I want to interpret as an unsigned int for some bit-twiddling (let’s assume sizeof(float) == sizeof(int) for now). Now there are various ways that “work”, for example:

float f = 0.5f;
unsigned int u;

u = * (unsigned int*) (&var); /* works in most compilers */

union {
    float f;
    unsigned int u;
} uconv;

uconv.f = f;
u = uconv.u; /* works in most compilers */

Now I don’t know the standard by heart, but IIRC the first is undefined behaviour thanks to the strict aliasing rule and I heard that the latter is undefined behaviour too.

So what is the correct, defined way to interpret the value of one type as if it was another type?


Please add to your answer whether it works for C89, C90, C99 or even C11.

  • 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-05-31T02:59:07+00:00Added an answer on May 31, 2026 at 2:59 am

    In general, the correct way is to cast the variable’s address to void*, char* or unsigned char*, because any other pointer cast may cause undefined behavior (indeed, because of strict aliasing). int* and float* are incompatible pointer types.

    When the sizeof both types is the same, the following will work:

    memcpy(&u, &var, sizeof(int));  // note: implicit conversion to void [const] *
    

    The value that you get is obviously implementation-defined, since it depends on how both int and float are represented in memory.

    Strict aliasing has been around since C89.

    EDIT: seems I was wrong previously about the union hack causing UB. The relevant part of the C11 draft states:

    If the member used to read the contents of a union object is not the same as the member last used to
    store a value in the object, the appropriate part of the object representation of the value is reinterpreted
    as an object representation in the new type as described in 6.2.6 (a process sometimes called ‘‘type
    punning’’).

    — Foonote 95 to 6.5.2.3 Structure and union members

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a float value say 28.980000. I want something like that 28.98, only
let's say I have a variable containing an integer or a float (since integers
Let's say I have a series of elements designed to render via float:left inside
I have a boost::mpl::vector with N elements, say: typedef boost::mpl::vector<int,float,double,short,char> my_vector; I wish to
Say I have a large number (integer or float) like 12345 and I want
Say I have a Float. I want the first 32 bits of the fractional
Let's say i have a method that get a GLuint texture as a parameter
Let's say I have a class like: class NavigationData { float roll; float pitch;
Let's say you have a float 5.9476386 and an NSArray with the floats 1,4,8,12,...
Let's say I have a class that I don't own: DataBuffer. It provides various

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.