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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T14:33:21+00:00 2026-05-15T14:33:21+00:00

i am interested on this problem Interleave bits the obvious way (from http://graphics.stanford.edu/~seander/bithacks.html )

  • 0

i am interested on this problem

Interleave bits the obvious way

(from http://graphics.stanford.edu/~seander/bithacks.html)

unsigned short x;   // Interleave bits of x and y, so that all of the
unsigned short y;   // bits of x are in the even positions and y in the odd;
unsigned int z = 0; // z gets the resulting Morton Number.

for (int i = 0; i < sizeof(x) * CHAR_BIT; i++) // unroll for more speed...
{
  z |= (x & 1U << i) << i | (y & 1U << i) << (i + 1);
}

can someone explain to me how this works with an example?

for example if we have x = 100101 and y = 010101, what will be result?

  • 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-15T14:33:22+00:00Added an answer on May 15, 2026 at 2:33 pm

    Bit interleaving essentially takes two n bit input numbers and produces one 2n bit output number that alternately takes bits from the two input numbers. That is, bits from one number goes into the odd indices, and bits from the other goes into the even indices.

    So for your specific example:

    x = 100101  =  1 0 0 1 0 1
    y = 010101  = 0 1 0 1 0 1
    
    interleaved = 011000110011
    

    Here we use the convention that bits from x goes into the even indices (0, 2, 4…) and bits from y goes into the odd indices (1, 3, 5…). That is, bit interleaving is not a commutative operation; interleave(x, y) is generally not equal to interleave(y, x).

    You can also generalize the bit interleaving operation to involve more than just 2 numbers.

    Bit-interleaved numbers exhibit structural properties that can be taken advantage of in many important spatial algorithms/data structures.

    See also

    • Wikipedia/Z-order (curve)
      • aka Morton-Order/Morton code

    Related questions

    • How to compute a 3D Morton number (interleave the bits of 3 ints)
    • How to de-interleave bits (UnMortonizing?)

    “Obvious” algorithm

    The algorithm essentially goes through every bits of the input numbers, checking if it’s 1 or 0 with bitwise-and, combining the two bits with bitwise-or, and concatenating them together with proper shifts.

    To understand how the bits are rearranged, just work on a simple 4-bit example. Here xi denotes the i-th (0-based) bit of x.

    x =    x3    x2    x1    x0
    y = y3    y2    y1    y0
                                             Mapping:
    z = y3 x3 y2 x2 y1 x1 y0 x0                 x[i] --> z[i+i]
        z7 z6 z5 z4 z3 z2 z1 z0                 y[i] --> y[i+i+1]
    

    Once you convinced yourself that the mapping pattern is correct, implementing it is simply a matter of understanding how bitwise operations are performed.

    Here’s the algorithm rewritten in Java for clarity (see also on ideone.com):

        int x = Integer.parseInt("100101", 2);
        int y = Integer.parseInt("010101", 2);
        int z = 0;
    
        for (int i = 0; i < Integer.SIZE; i++) {
           int x_masked_i = (x & (1 << i));
           int y_masked_i = (y & (1 << i));
    
           z |= (x_masked_i << i);
           z |= (y_masked_i << (i + 1));
        }
        System.out.println(Integer.toBinaryString(z));
        // prints "11000110011"
    

    See also

    • Wikipedia/Bitwise operations
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

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.