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

  • Home
  • SEARCH
  • 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 9116775
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T04:45:16+00:00 2026-06-17T04:45:16+00:00

In the following code #include <iostream> using namespace std; struct field { unsigned first

  • 0

In the following code

#include <iostream>

using namespace std;
struct field
{
   unsigned first : 5;
   unsigned second : 9;
};
int main()
{
   union
   {
      field word;
      int i;
   };
   i = 0;
   cout<<"First is : "<<word.first<<" Second is : "<<word.second<<" I is "<<i<<"\n";
   word.first = 2;
   cout<<"First is : "<<word.first<<" Second is : "<<word.second<<" I is "<<i<<"\n";
   return 0;
}

when I initialize word.first = 2, as expected it updates 5-bits of the word, and gives the desired output. It is the output of ‘i’ that is a bit confusing. With word.first = 2, i gives output as 2, and when I do word.second = 2, output for i is 64. Since, they share the same memory block, shouldnt the output (for i) in the latter case be 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-17T04:45:17+00:00Added an answer on June 17, 2026 at 4:45 am

    This particular result is platform-specific; you should read up on endianness.

    But to answer your question, no, word.first and word.second don’t share memory; they occupy separate bits. Evidently, the underlying representation on your platform is thus:

    bit   15 14 13 12 11 10  9  8  7  6  5  4  3  2  1  0
         +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
         |     |         second           |    first     |
         +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
         |<------------------- i ----------------------->|
    

    So setting word.second = 2 sets bit #6 of i, and 26 = 64.

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

Sidebar

Related Questions

The following code #include <iostream> using namespace std; int main() { const char* const
Consider the following code: #include <iostream> using namespace std; int main() { int x,
I have the following code: #include <iostream> using namespace std; struct stud{ int roll;
the following code #include <iostream> using namespace std; int main(){ char greeting[50] = goodmorning
Suppose the following c++ code: #include <iostream> using namespace std; typedef struct { int
I have the following code: #include <iostream> using namespace std; struct S { S(int
I have the following code: #include <iostream> #include <vector> using namespace std; struct A{};
Consider the following code: #include <iostream> #include <memory> #include <vector> using namespace std; struct
I have the following code: #include<iostream> using namespace std; typedef void (*HandlerFunc)(int, int); HandlerFunc
I have the following code #include <iostream> #include<string> #include <sstream> using namespace std; struct

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.