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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T03:49:59+00:00 2026-06-05T03:49:59+00:00

I have a struct with bit-fields (totally 32 bit width) and I have a

  • 0

I have a struct with bit-fields (totally 32 bit width) and I have a 32-bit variable. When I try to assign the variable value to my struct, I got an error:

error: conversion from ‘uint32_t {aka unsigned int}’ to non-scalar type ‘main()::CPUID’ requested.

struct CPUIDregs
    {
       uint32_t EAXBuf;
    };
CPUIDregs CPUIDregsoutput;   


int main () {

 struct CPUID          
    {
          uint32_t   Stepping         : 4;         
          uint32_t   Model            : 4;        
          uint32_t   FamilyID         : 4;        
          uint32_t   Type             : 2;        
          uint32_t   Reserved1        : 2;         
          uint32_t   ExtendedModel    : 4;         
          uint32_t   ExtendedFamilyID : 8;          
          uint32_t   Reserved2        : 4;          
    };

    CPUID CPUIDoutput = CPUIDregsoutput.EAXBuf;

Do you have any idea how to do it in the shortest way? Thanks

P.S. Of course I have more appropriate value of EAX in real code, but I guess it doesn’t affect here.

  • 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-05T03:50:01+00:00Added an answer on June 5, 2026 at 3:50 am

    You should never rely on how the compiler lays out your structure in memory. There are ways to do what you want with a single assignment, but I will neither recommend nor tell you.

    The best way to do the assignment would be the following:

    static inline void to_id(struct CPUid *id, uint32_t value)
    {
        id->Stepping         = value & 0xf;
        id->Model            = value >> 4 & 0xf;
        id->FamilyID         = value >> 8 & 0xf;
        id->Type             = value >> 12 & 0x3;
        id->Reserved1        = value >> 14 & 0x3;
        id->ExtendedModel    = value >> 16 & 0xf;
        id->ExtendedFamilyID = value >> 20 & 0xff;
        id->Reserved2        = value >> 28 & 0xf;
    }
    

    And the opposite

    static inline uint32_t from_id(struct CPUid *id)
    {
        return id->Stepping
             | id->Model << 4
             | id->FamilyID << 8
             | id->Type << 12
             | id->Reserved1 << 14
             | id->ExtendedModel << 16
             | id->ExtendedFamilyID << 20
             | id->Reserved2 << 28;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have this struct (which incidentally contain bit-fields, but you shouldn't care): struct
I have a code which uses bit-fields declared as follows typedef struct my{ const
I have a program that compares variables from two structs and sets a bit
Is it possible to marshal a C-style struct containing bit-fields to a C# struct,
I have this code : public struct SmartFilter { public int from, to; public
Basically, I have a struct which contains a number of fields. I'm going to
I do have a structure having bit-fields in it.Its comes out to be 2
Ok so I have struct like this typedef struct { float x; float y;
Suppose I have: struct Magic { Magic(Foo* foo); Magic(Bar* bar); }; Is there a
using C++, I have struct Base { virtual void stuff(/*base stuff*/); }; struct Derived

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.