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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T17:34:43+00:00 2026-06-09T17:34:43+00:00

i run this code in c++: #include <iostream> using namespace std; int main() {

  • 0

i run this code in c++:

#include <iostream>
using namespace std;
int main()
{
    float f = 7.0;
    short s = *(short *)&f;
    cout << sizeof(float) << endl
         << sizeof(short) << endl
         << s << endl;
    return 0;
}

i get the following out pot:

4
2
0

but, in a lecture given in Stanford university, Professor Jerry Cain says he is sure the out pot well not be 0.

the lecture is can be fond here. he says that around the 48 minute.

is he wrong, or that some standard change since? or is there a difference between platforms?
I’m using g++ to compile my code.

EDIT: in the next lecture he does mention “big endian” and “small endian” and says that they well affect the result.

  • 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-09T17:34:44+00:00Added an answer on June 9, 2026 at 5:34 pm
    static void bitPrint(float f)
    {
        assert(sizeof(int) == sizeof(float));
        int *data = reinterpret_cast<int*>(&f);
        for (int i = 0; i < sizeof(int) * 8; ++i)
        {
            int bit = (1 << i) & *data;
            if (bit) bit = 1;
            cout << bit;
        }
        cout << endl;
    }
    
    int main()
    {
        float f = 7.0;
        bitPrint(f);
        return 0;
    }
    

    This program prints 00000000000000000000011100000010

    Since the sizeof(short) == 2 on your platform you get the first 2 bytes which are both zeros

    Note that since size of types and possibly float implementation (not sure about this) are implementation defined different output can be seen on different platforms.

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

Sidebar

Related Questions

This is my code : #include <iostream> using namespace std; int main() { long
When I compile and run this code: #include <iostream> using namespace std; class Base
#include <iostream> using namespace std; int main () { cout << Hello World!; return
Consider the following code: #include<iostream> using namespace std; class sample { int a; int
Consider the following sample code: #include <iostream> using namespace std; class base { public:
The following piece of code #include <iostream> #include <vector> #include <tuple> using namespace std;
I have this code: #define _WIN32_WINNT 0x0500 #include <cstdlib> #include <iostream> #include <windows.h> using
I have this code: #define _WIN32_WINNT 0x0500 #include <cstdlib> #include <iostream> #include <windows.h> using
Question #1: if I have a C++ code like this #include <iostream> using namesapce
I have this code : #include <iostream> #include <stdio.h> #include <pthread.h> #include <stdlib.h> using

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.