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

The Archive Base Latest Questions

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

Is there an elegant way of moving a bit within a byte (or word/long).

  • 0

Is there an elegant way of moving a bit within a byte (or word/long). For simplicity, lets use a simple 8-bit byte and just one bit to move within the byte.

Given a bit number, based on 0-7 Least-sig-bit to most-sig-bit, (or bits 1-8 if you’d rather), I would like to move a bit from one position to another:

7654 3210 <bit position
0101 1010 <some binary value
--x- --y- <move bit from x to y
0111 0100 <new value with x moved to y and intervening bits shifted left

So, x at bit position 5 moves to y at bit position 1, bits 0,6,7 stay unchanged. Bits 2,3,4 are shifted left to ‘make room’ for the bit moved from 5 to 2. This is just an example.

It is important that the bit moves, not swapped with its target. There are numerous exampls of bits that swap, but that is quite trivial.

The solution ideally would use simple bit-twiddling and bitwise operators. Assume language agnostic, bit simple AND/OR/XOR, NOT, SHIFT Left/Right / ROTATE or similar instructions would be fine in any combination, plus any other basic arithmetic operator, eg: mod, addition/subtraction etc. Even working psuedo-code would be ok. Alternatively, a bit array or bitfield type structure would probably be straightforward.

In addition to the actual bit move, I would like to find a way to :

  • Move any bit up or down.
  • Specify the bit number source/destination in any convenient format: eg: 6>2
    implies shift down, 3>7 shift up or start-bit +/- offset: 6-4 or 3+4, or bit weighted: bit 6=64 to bit 3=8.
  • Possibly extendable from byte to unsigned int, long, etc.
  • (Ideally, be extendable
    to more than one bit at a time, probably adjacent bits if easier)

Performance is not a major issue, but something elegant is likley to be plenty fast enough.

My own niaive approach would be to identify the source and target bit positions, decide if shift up or down, take a shifted copy, mask off the static bits and find the source bit, merge the static and shifted bits and somehow set/clear the target bit. However, while the theory seems good, an elegant implementation is beyond me.

I realise that a precompiled lookup table could be built for a byte, but if this is to be extended to integers/longs, this would be impractical for me.

Any help appreciated. Thanks in advance.

  • 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-23T08:25:15+00:00Added an answer on May 23, 2026 at 8:25 am

    First, an observation about the original problem, and the subsequent extensions that you mention:

    The “moving a bit” operation that you describe is really a rotation of a contiguous range of bits. In your example, you are rotating bits 1-5 inclusive, by one bit to the left:

      7   6   5   4   3   2   1   0          7   6   5   4   3   2   1   0
    +---+---+---+---+---+---+---+---+      +---+---+---+---+---+---+---+---+
    | 0 | 1 | 0<--1<--1<--0<--1 | 0 |  ->  | 0 | 1 | 1 | 1 | 0 | 1 | 0 | 0 |
    +---+---+-|-+---+---+---+-^-+---+      +---+---+---+---+---+---+---+---+
              |               |
              +---------------+
    

    If you consider a more general form of this operation to be “rotate a range of bits left by some amount” with three parameters:

    1. the least significant bit to include in the rotation
    2. the most significant bit to include in the rotation
    3. the number of bits to rotate by

    then it becomes a single basic primitive which can perform all of the things you want to do:

    • you can obviously move any bit (choose appropriate least/most significant bit paramaters);
    • you can rotate left or right, because if you are rotating a range of n bits, then a rotation right by k bits is the same thing as a rotation left by n – k bits;
    • it trivially generalises to any bit width;
    • by definition we can rotate more by more than one bit at a time.

    So now, all that’s needed is to construct this primitive…


    To start with, we’re almost certainly going to need a bit mask for the bits we care about.

    We can form a mask for bits 0 – n by shifting a 1 by n + 1 bits to the left, then subtracting 1. e.g. a mask for bits 0-5 would be (in binary):

    00111111
    

    …which can be formed by taking a 1:

    00000001
    

    …shifting 5+1 = 6 bits to the left:

    01000000
    

    …and subtracting 1 to give:

    00111111
    

    In C, this would be (1 << (bit + 1)) - 1. But there is a subtlety here, for C at least (and I apologise for the digression when you’ve tagged this as language-agnostic, but this is important, and there are probably similar issues in other languages too): a shift by the width of your type (or more) leads to undefined behaviour. So if we were trying to construct a mask for bits 0-7 for an 8-bit type, the calculation would be (1 << 8) - 1, which would be undefined. (It might work on some systems and some compilers, but wouldn’t be portable.) There are also undefined behaviour issues with signed types in the case where you would end up shifting into the sign bit.

    Fortunately, in C, we can avoid these problems by using an unsigned type, and writing the expression as (1 << bit) + (1 << bit) - 1. Arithmetic with unsigned n-bit values is defined by the standard to be reduced modulo 2n, and all of the individual operations are well-defined, so we’re guaranteed to get the right answer.

    (End of digression.)

    OK, so now we have a mask for bits 0 – msb. We want to make a mask for bits lsb – msb, which we can do by subtracting the mask for bits 0 – (lsb-1), which is (1 << lsb) - 1. e.g.

      00111111      mask for bits 0-5:  (1 << 5) + (1 << 5) - 1
    - 00000001      mask for bits 0-0:  (1 << 1) - 1
      --------                         -------------------------------
      00111110      mask for bits 1-5:  (1 << 5) + (1 << 5) - (1 << 1)
    

    So the final expression for the mask is:

    mask = (1 << msb) + (1 << msb) - (1 << lsb);
    

    The bits to be rotated can be selected by a bitwise AND with the mask:

    to_rotate = value & mask;
    

    …and the bits that will be left untouched can be selected by a AND with the inverted mask:

    untouched = value & ~mask;
    

    The rotation itself can be performed easily in two parts: first, we can obtain the leftmost bits of the rotated portion by simply rotating to_rotate left and discarding any bits that fall outside the mask:

    left = (to_rotate << shift) & mask;
    

    To get the rightmost bits, rotate to_rotate right by (n – shift) bits, where n is the number of bits we’re rotating (this n can be calculated as msb + 1 - lsb):

    right = (to_rotate >> (msb + 1 - lsb - shift)) & mask;
    

    The final result can be obtained by combining all the bits from untouched, left, and right:

    result = untouched | left | right;
    

    Your original example would work like this (msb is 5, lsb is 1, and shift is 1):

        value = 01011010
    
        mask  = 00111110   from (1 << 5) + (1 << 5) - (1 << 1)
    
                01011010   value
              & 00111110   mask
              ----------
    to_rotate = 00011010
    
                01011010   value
              & 11000001   ~mask  (i.e. inverted mask)
              ----------
    untouched = 01000000
    
                00110100   to_rotate << 1
              & 00111110   mask
              ----------
         left = 00110100
    
                00000001   to_rotate >> 4  (5 + 1 - 1 - 1 = 4)
              & 00111110   mask
              ----------
        right = 00000000
    
                01000000   untouched
                00110100   left
              | 00000000   right
              ----------
       result = 01110100
    

    Here’s a different example with a 16-bit input value, msb = 15, lsb = 4, and shift = 4 (which rotates the top 3 hex digits of a 4-digit hex value):

        value = 0101011001111000   (0x5678)
    
        mask  = 1111111111110000   from (1 << 15) + (1 << 15) - (1 << 4)
    
                0101011001111000   value
              & 1111111111110000   mask
              ------------------
    to_rotate = 0101011001110000
    
                0101011001111000   value
              & 0000000000001111   ~mask
              ------------------
    untouched = 0000000000001000
    
                0110011100000000   to_rotate << 4
              & 1111111111110000   mask
              ------------------
         left = 0110011100000000
    
                0000000001010110   to_rotate >> 8  (15 + 1 - 4 - 4 = 8)
              & 1111111111110000   mask
              ------------------
        right = 0000000001010000
    
                0000000000001000   untouched
                0110011100000000   left
              | 0000000001010000   right
              ------------------
       result = 0110011101011000   =  0x6758
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to test a class that use random.nextDouble() is there elegant way to
This is probably a bit low question, but is there any elegant way, how
Is there a elegant way of obtaining only one Entry<K,V> from HashMap, without iterating,
is there an elegant way to start a 64 bit process from an 32
Is there an elegant way to use a specific dependency as a file object
Is there any elegant way to make Java method located within parent class return
Is there an elegant way to specialize a template based on one of its
Is there an elegant way in java to check if an int is equal
Is there an elegant way to make whitespace string. To be more precise, I'm
Is there an elegant way to do this in MySQL: SELECT (subquery1) AS s1,

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.