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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T07:56:35+00:00 2026-06-09T07:56:35+00:00

I am trying to make a class that will encode 4 values in one

  • 0

I am trying to make a class that will encode 4 values in one 32-bit integer. So far, it saves and outputs all the values without losing anything, but for some reason, in the wrong order.

struct encoder {
    uint32_t val;

    encoder(uint32_t _val = 0) : val(_val) {}

    uint32_t first(uint32_t v = 0) {
        if (!v) return (val << (8*3)) >> (8*3);

        val |= v;
    }

    uint32_t second(uint32_t v = 0) {
        if (!v) return (val << (8*2)) >> (8*3);

        encoder _backupval(val);

        val = (val >> (8*1));
        val |= v;
        val = (val << (8*1));
        val |= _backupval.first();
    }

    uint32_t third(uint32_t v = 0) {
        if (!v) return (val << (8*1)) >> (8*3);

        encoder _backupval(val);

        val = (val >> (8*2));
        val |= v;
        //now restore
        val = val << 8;
        val |= _backupval.second();
        val = val << 8;
        val |= _backupval.first();
    }

    uint32_t fourth(uint32_t v = 0) {
        if (!v) return (val << (8*0)) >> (8*3);

        encoder _backupval(val);

        val = (val >> (8*3));
        val |= v;
        //now restore
        val = val << 8;
        val |= _backupval.second();
        val = val << 8;
        val |= _backupval.first();
        val = val << 8;
        val |= _backupval.third();
    }
};

int main() {
    encoder t;
    t.first(6);
    t.second(42);
    t.third(212);
    t.fourth(23);

    cout << "first number: " << t.first()
         << "\nsecond number: " << t.second()
         << "\nthird number: " << t.third()
         << "\nfourth number: " << t.fourth() << endl;

}

Note that I am doing this as an exercise, and this program is not going to be used in real programs. So do not suggest alternative ways to do it, and do not point out the flaws (like if the size of the number is more then 256, it will corrupt the other integer, etc.)

  • 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-09T07:56:37+00:00Added an answer on June 9, 2026 at 7:56 am

    You mixed the order of functions in fourth:

    uint32_t fourth(uint32_t v = 0) {
        if (!v) return (val << (8*0)) >> (8*3);
    
        encoder _backupval(val);
    
        val = (val >> (8*3));
        val |= v;
        //now restore
        val = val << 8;
        val |= _backupval.second();
        val = val << 8;
        val |= _backupval.first();
        val = val << 8;
        val |= _backupval.third();  // this should be called first!
    }
    

    This will result in the scrambled return values. Simply call third first and it will work:

    uint32_t fourth(uint32_t v = 0) {
        if (!v) return (val << (8*0)) >> (8*3);
    
        encoder _backupval(val);
    
        val = (val >> (8*3));
        val |= v;
        //now restore
        val = val << 8;
        val |= _backupval.third();
        val = val << 8;
        val |= _backupval.second();
        val = val << 8;
        val |= _backupval.first();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to make a class that will represent two different behaivours at
I'm trying to build a small class-like container that will make it a little
A am trying to make a program that takes a signal from one class
I'm just trying to make a simple function that will return all the data
I am trying to create an form validator class that will check to make
I am trying to make a method for a class that will automatically insert
I'm trying to make a class that will handle some router functionality such as
http://jsfiddle.net/JamesKyle/re73T/ Trying to make a class switcher that will do the following: On Load
I am trying to extend the mysqli class to make a helper class that
I've been trying to make a generic class to represent a range of values

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.