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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T22:04:47+00:00 2026-05-13T22:04:47+00:00

In C/C++, is there an easy way to apply bitwise operators (specifically left/right shifts)

  • 0

In C/C++, is there an easy way to apply bitwise operators (specifically left/right shifts) to dynamically allocated memory?

For example, let’s say I did this:

unsigned char * bytes=new unsigned char[3];
bytes[0]=1;
bytes[1]=1;
bytes[2]=1;

I would like a way to do this:

bytes>>=2;

(then the ‘bytes’ would have the following values):

bytes[0]==0
bytes[1]==64
bytes[2]==64

Why the values should be that way:

After allocation, the bytes look like this:

[00000001][00000001][00000001]

But I’m looking to treat the bytes as one long string of bits, like this:

[000000010000000100000001]

A right shift by two would cause the bits to look like this:

[000000000100000001000000]

Which finally looks like this when separated back into the 3 bytes (thus the 0, 64, 64):

[00000000][01000000][01000000]

Any ideas? Should I maybe make a struct/class and overload the appropriate operators? Edit: If so, any tips on how to proceed? Note: I’m looking for a way to implement this myself (with some guidance) as a learning experience.

  • 1 1 Answer
  • 1 View
  • 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-13T22:04:47+00:00Added an answer on May 13, 2026 at 10:04 pm

    I’m going to assume you want bits carried from one byte to the next, as John Knoeller suggests.

    The requirements here are insufficient. You need to specify the order of the bits relative to the order of the bytes – when the least significant bit falls out of one byte, does to go to the next higher or next lower byte.

    What you are describing, though, used to be very common for graphics programming. You have basically described a monochrome bitmap horizontal scrolling algorithm.

    Assuming that “right” means higher addresses but less significant bits (ie matching the normal writing conventions for both) a single-bit shift will be something like…

    void scroll_right (unsigned char* p_Array, int p_Size)
    {
      unsigned char orig_l = 0;
      unsigned char orig_r;
    
      unsigned char* dest = p_Array;
    
      while (p_Size > 0)
      {
        p_Size--;
    
        orig_r  = *p_Array++;
        *dest++ = (orig_l << 7) + (orig_r >> 1);
    
        orig_l = orig_r;
      }
    }
    

    Adapting the code for variable shift sizes shouldn’t be a big problem. There’s obvious opportunities for optimisation (e.g. doing 2, 4 or 8 bytes at a time) but I’ll leave that to you.

    To shift left, though, you should use a separate loop which should start at the highest address and work downwards.

    If you want to expand “on demand”, note that the orig_l variable contains the last byte above. To check for an overflow, check if (orig_l << 7) is non-zero. If your bytes are in an std::vector, inserting at either end should be no problem.

    EDIT I should have said – optimising to handle 2, 4 or 8 bytes at a time will create alignment issues. When reading 2-byte words from an unaligned char array, for instance, it’s best to do the odd byte read first so that later word reads are all at even addresses up until the end of the loop.

    On x86 this isn’t necessary, but it is a lot faster. On some processors it’s necessary. Just do a switch based on the base (address & 1), (address & 3) or (address & 7) to handle the first few bytes at the start, before the loop. You also need to special case the trailing bytes after the main loop.

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

Sidebar

Related Questions

Is there an easy way to apply a low-pass or high-pass filter to an
Is there an easy way (not using loops) to apply -like with more than
Is there an easy way to log PHP errors to a log for a
Is there an easy way to generate docs for REST api direct from a
Is there an easy way of getting the datetime of the last change to
Is there an easy way in javascript to only escape certain ranges of control
Is there an easy way to calculate the derivative of non-liner functions that are
Is there an easy way to remove the character at a certain position in
Is there an easy way to use UIElement.Effect in WPF to shift the color
Is there an easy way to be able to send an email at a

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.