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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T23:16:58+00:00 2026-06-17T23:16:58+00:00

OK, let’s consider a 64-bit number, with its bits forming a 8×8 table. E.g.

  • 0

OK, let’s consider a 64-bit number, with its bits forming a 8×8 table.

E.g.

0 1 1 0 1 0 1 0 0 1 1 0 1 0 1 1 0 1 1 1 1 0 1 0 0 1 1 0 1 0 1 0 1 1 1
0 1 0 1 0 0 1 1 0 1 0 1 0 0 1 1 0 1 1 1 0 0 1 1 0 1 0 1 0

written as

a b c d e f g h
----------------
0 1 1 0 1 0 1 0
0 1 1 0 1 0 1 1 
0 1 1 1 1 0 1 0 
0 1 1 0 1 0 1 0 
1 1 1 0 1 0 1 0 
0 1 1 0 1 0 1 0 
0 1 1 0 1 1 1 0 
0 1 1 0 1 0 1 0

Now, what if we want to isolate JUST e.g. column d (00100000) (or any row/diagonal for that matter) ?

Can this be done? And if so, how?


HINTS :

  • (a) My main objective here – though not initially mentioned – is raw speed. I’m searching for the fastest algorithm around, since the “retrieval” function is being performed some millions of times per second.

  • (b) This is what comes closer to what I mean : https://www.chessprogramming.org/Kindergarten_Bitboards

  • 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-17T23:16:58+00:00Added an answer on June 17, 2026 at 11:16 pm

    Here’s a solution with only 4 main steps:

    const uint64_t column_mask = 0x8080808080808080ull;
    const uint64_t magic = 0x2040810204081ull;
    
    int get_col(uint64_t board, int col) {
        uint64_t column = (board << col) & column_mask;
        column *= magic;
        return (column >> 56) & 0xff;
    }
    

    It works like this:

    • the board is shifted to align the column with the left side
    • it’s masked to only contain the required column (0..8)
    • it’s multiplied by a magic number which results in all the original bits pushed to the left side
    • the left-most byte is shifted to the right

    The magic number is chosen to copy only the needed bits and let the rest fall into unused places / overflow over the number. The process looks like this (digits are bit “IDs”, rather than the number itself):

    original column: ...1.......2.......3.......4.......5.......6.......7.......8....
    aligned column:  1.......2.......3.......4.......5.......6.......7.......8.......
    multiplied:      123456782345678.345678..45678...5678....678.....78......8.......
    shifted to right:........................................................12345678
    

    If you add the const keywords, assembly becomes quite nice actually:

    get_col:
    .LFB7:
            .cfi_startproc
            movl    %esi, %ecx
            movabsq $-9187201950435737472, %rax
            salq    %cl, %rdi
            andq    %rax, %rdi
            movabsq $567382630219905, %rax
            imulq   %rax, %rdi
            shrq    $56, %rdi
            movl    %edi, %eax
            ret
    

    No branching, no external data, around 0.4ns per calculation.

    Edit: takes around 6th of the time using NPE’s solution as baseline (next fastest one)

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

Sidebar

Related Questions

Let's say I don't have photoshop, but I want to make pattern files (.pat)
Let's say I have a table with a Color column. Color can have various
Let's say I have thousands of users and I want to make the passwords
Let's say I have a 1 GB text file and I want to read
Let's say you have a class library project that has any number of supplemental
Let's say I want to write a function that validates an email address with
Let's say that each Product has a category. I want to ask the Users
Let's say I have a file foo.py, and within the file I want to
Let's say I want to write an FTP client from scratch. In the command
Let's say that I've got a sheet - number one - with over 5000

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.