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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T09:13:25+00:00 2026-05-23T09:13:25+00:00

are these correct implementations of the operator<< and >> for bitshifting? for some reason,

  • 0

are these correct implementations of the operator<< and >> for bitshifting? for some reason, in my operator/, where it only involves these 2 operators, something is allowing for a silly jump that is causing division to be done incorrectly.

    // the value is stored msb in a list of uint8_t
    // so 0x123456 will be stored in the list as {0x12, 0x34, 0x56}
    integer operator<<(size_t shift){
        std::list <uint8_t> out = value;
        for(unsigned int i = 0; i < (shift >> 3); i++)
            out.push_back(0);
        shift &= 7;
        if (shift){
            out.push_front(0);
            std::list <uint8_t>::iterator i = out.begin(), j = out.end();
            i++; j--;
            for(; i != j; i++){
                uint8_t temp = *i >> (8 - shift);
                --i;
                *i += temp;
                i++;
                *i = (uint8_t) (*i << shift);
            }
            uint8_t temp = *i >> (8 - shift);
            i--;
            *i += temp;
            i++;
            *i <<= shift;
        }
        return integer(out);
    }

    integer operator>>(size_t shift){
        std::list <uint8_t> out = value;
        for(unsigned int i = 0; i < (shift >> 3); i++)
            out.pop_back();
        shift &= 7;
        if (shift){
            std::list <uint8_t>::reverse_iterator i = out.rbegin(), j = out.rend();
            j--;
            for(; i != j; i++){
                *i >>= shift;
                i++;
                uint8_t temp = *i << (8 - shift);
                i--;
                *i += temp;
            }
            *j >>= shift;
        }
        return integer(out);
    }

heres what im doing:

integer(1234567) / integer(6)

inside division algorithm (long division):

numerator       largest multiple of denomiator < numeator

12d687          0c0000
06d687          060000
d687            c000
1687            0c00
0a87            0600
0487            0300
0187            0c        <-- where did this come from??
017b            0180

is the error in one of the operators shown or in some other operator?

heres my entire code: http://ideone.com/ncq9S

  • 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-23T09:13:26+00:00Added an answer on May 23, 2026 at 9:13 am
    template <typename T>
    

    Does this really need to be parameterized? It would seem that any shift quantity can be stored in a size_t, and converting to size_t implicitly or explicitly would be a good idea before starting the rest of the process.

    integer operator<<(T shift){
    

    Usually binary operator functions are best implemented as non-member friends. For example, fewer conversions are considered when producing the this pointer than for generic operands. I’d write friend integer operator<< ( integer lhs, size_t shift ).

        std::list <uint8_t> out = value;
    

    If you use a friend and pass-by-value, then this copy is made implicitly. The compiler also has an easier time eliminating it if you are really just modifying one object, e.g. q = q << 3.

        for(unsigned int i = 0; i < (shift >> 3); i++)// get rid of bytes if shift > 8
    

    Be careful in the loop condition. You’re applying an operator to a high-level type, which might cause an expensive computation (or even endless recursion if T is integer!

        shift &= 7;                                   // shift by less than a byte
    

    At this point, if shift == 0 you are done. Best to insert a conditional return here. Also, now the range is really limited, so assign to a narrower type than T.

        out.push_front(0);                            // extra byte for overflow
    

    This is only necessary if the byte will be nonzero. Probably best to special-case the first operation and make the push_front conditional, instead of special-casing the last.

    … Hmm, it looks like the operator>> is being exercised more… skipping to that…

        for(; i != j; i++){
            i++;
            uint8_t temp = *i << (8 - shift);
            i--;
            *i += temp;
        }
        *j >>= shift;
    

    Clearly the >> operator is missing from the loop. Try *i = *i >> shift + temp. Also, I don’t see how the list ever gets shorter. Is that done in integer::integer( list<…> )?

    However, I can’t really see what produces the behavior in the posted output. The leading zero is probably a result of the unconditional push_front in operator<<, but the pattern I’d expect is c, 6, 3, 18, c, …. You don’t seem to be repeating any sequence, but instead jump randomly.

    Maybe the division code, or the class constructor could provide clues.

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

Sidebar

Related Questions

I'm trying to find the correct names for these 2 types of coding expressions
I followed these instructions here: http://logback.qos.ch/consolePlugin.html I have the correct and found logback.xml, it
What is the correct way of iterating over a vector in C++? Consider these
Firstly I know that there are many question and solutions to correct thread marshalling
I have read the definitions of these terms and my interpretation is that there
I am writing a Graph library that has both adjacency list and matrix implementations.
I have a TTLauncherView with some TTLauncherItems . These show badges, representing messages from
I'm very new to multi-threading and for some reason this class is giving me
In our application, some of our REST-style WCF calls are failing. These calls are
I have an interface and there are couple of implementations for that Interface. Now

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.