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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:15:00+00:00 2026-05-27T09:15:00+00:00

Suppose we have a binary file, that contains 32 bit numbers. Each 32bit number

  • 0

Suppose we have a binary file, that contains 32 bit numbers. Each 32bit number represents an instruction. My question: is it possible to cut this bits into chunks of 6+5+5+16 directly. Something like:

typedef struct _instruction
{
    int op_code : 6;
    int reg_dest : 5;
    int reg_s1 : 5;
    int offset : 16;
} INST, *PINST;

int read_32_bits = read_next_instr();

INST i = (INST)read_32_bit; /* this would cut the bits into chunks*/
  • 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-27T09:15:01+00:00Added an answer on May 27, 2026 at 9:15 am

    Here is an answer that will work, be portable, not invoke undefined behavior on any compiler, and be optimized fairly effectively:

    struct instruction {
       typedef unsigned int uint_t;
    
       explicit instruction(uint_t val) : val_(val) {}
       instruction(uint_t op_code, uint_t reg_dest, uint_t reg_s1, uint_t offset)
         : val_((op_code & 0x3fu << 26) | (reg_dest & 0x1fu << 21) |
                (reg_s1 & 0x1fu << 16) | (offset & 0xffffu))
       {
       }
    
       uint_t op_code() const { return (val_ >> 26) & 0x3fu; }
       void op_code(uint_t newval) { val_ = (newval & 0x3fu << 26) | (val_ & 0x3ffffffu); }
    
       uint_t reg_dest() const { return (val_ >> 21) & 0x1fu; }
       void reg_dest(uint_t newval) { val_ = (newval & 0x1fu << 21) | (val_ & 0xfc1fffffu); }
    
       uint_t reg_s1() const { return (val_ >> 16) & 0x1fu; }
       void reg_s1(uint_t newval) { val_ = (newval & 0x1fu) << 16) | (val_ & 0xffe0ffffu); }
    
       uint_t offset() const { return (val_ >> 16) & 0xffffu; }
       void offset(uint_t newval) const { val_ = (newval & 0xffffu) | (val & 0xffff0000u); }
    
       uint_t &int_ref() { return val_; }
       uint_t int_ref() const { return val_; }
    
     private:
       uint_t val_;
    };
    

    This lets you access all of the bitfields with a very convenient notation. I think it’s also a POD, which lets you use it in a few interesting ways. And a good compiler will do a fairly decent job of optimizing the bit munging operations, especially if you have several calls to the convenience functions in a row.

    It’s almost as nice as having an overlay bit field. It’s just a bit more work to define in the first place.

    Also, I changed the type to unsigned int because if you’re fiddling around with the bits, you really want a simply represented number without a sign bit or anything funky like that. Ideally you’d be including the <cstdint> header and using ::std::uint32_t or something in the typedef at the top.

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

Sidebar

Related Questions

Suppose I have a struct and a file with binary representations of those structs
Suppose I have an ELF binary that's dynamic linked, and I want to override/redirect
Suppose I have a stringbuilder in C# that does this: StringBuilder sb = new
Suppose I have: Toby Tiny Tory Tily Is there an algorithm that can easily
Suppose I have a table called Companies that has a DepartmentID column. There's also
I was just curious I have a binary executable file in unix around 9MB.
Hey everyone, I have written a script that downloads a zip file from a
Ok, so I have a function that takes the path to an image file
Short and easy question, but I seem to have a writer's block here: Suppose
I have a program that takes two files as an argument. The first file

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.