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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T16:49:55+00:00 2026-05-28T16:49:55+00:00

Hello all I have a problem with understanding the bitsort program in Bentley’s classic

  • 0

Hello all I have a problem with understanding the bitsort program in Bentley’s classic programming pearls. I am new to Bitmask and Bitset, so I am not able to understanding these concepts.
Actually the program is for “How to sort a disk file?”.So below is the code

#include <stdio.h>
#define BITSPERWORD 32
#define SHIFT 5
#define MASK 0x1F
#define N 10000000
int a[1 + N/BITSPERWORD];

void set(int i) {
    a[i>>SHIFT] |=  (1<<(i & MASK));
}

void clr(int i) {
    a[i>>SHIFT] &= ~(1<<(i & MASK)); 
}
int  test(int i){ 
    return a[i>>SHIFT] &   (1<<(i & MASK)); 
}

int main()
{   int i;
    for (i = 0; i < N; i++)
        clr(i);
/*  Replace above 2 lines with below 3 for word-parallel init
    int top = 1 + N/BITSPERWORD;
    for (i = 0; i < top; i++)
        a[i] = 0;
 */
    while (scanf("%d", &i) != EOF)
        set(i);
    for (i = 0; i < N; i++)
        if (test(i))
            printf("%d\n", i);
    return 0;
}

Could some one please explain this code to me? If possible please provide a version in Java. Actually, I am comfortable in Java only so that why I am asking. This is not homework.

Possible duplicates: link1 link2

  • 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-28T16:49:56+00:00Added an answer on May 28, 2026 at 4:49 pm

    We’re given the numbers are going to lie between 0 to N,

    So we make a large BitSet, which is essential a large boolean array (working explained at the end) but takes less memory (each bit is technically a boolean)

    So what Jon does, he sets the entire bit set to false,then for each number encountered, he sets that Bit to true….finally, he runs through the bitset and for each true entry he prints the index. This will sort an array where we know an element always lies between 0 to N.

    Note : The above algorithm will fail with duplicates.

    Now for the Bit Mask stuff…

    Say i have a integer array (sizeof(int) = 32) But i want to use it like a boolean array of N size. So how many elements do i really need? It’s N/32

    int a[1 + N/BITSPERWORD]; // allocates BitSet of N size
    

    Now if I want to access ith element of the BitSet here’s how the indexing works.

    Eg, i = 49

    so a[0] contains bits 0-31, a[1] contains 32-63.

    a[i/32] (gives you which int array element contains the bit)
    and i % 32 the bit position within that element.

    so for i= 49, a[49/32] & ( 1 << (i % 32) ) will tell you if the 49th bit is set or not.

    If you are familiar with bitwise optimizations, you know that dividing by factors of 2, is essentially, shifting the number right by the number of factors.

    32 = 2^5…therefore X/32 same as X >> 5

    also X % 32 is same as X & 0x1f

    test function gives out 1 if the bitset at the position is set,

    clr clears the bitset at that position to zero

    set sets the bitset at the position to one.

    Phew! Hope that helps.

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

Sidebar

Related Questions

Problem Hello all! I have this code which takes my jpg image loops through
Hello all i have a simple problem i have a alertDialog and i want
Hello all and thanks for the attention! I have a problem that must both
Hello all my problem is : We have to create a system in which
Hello to all that read I have a small problem(or it could be large!),
hello all I have a small dialog which I created dynamically, which has a
I have a problem with modprobe command... I compiled the hello world module and
hello all i want to retrieve my all the wall post that i have
I have problem in displaying drop-down menu. Please, see the example on: http://staging.gwynconsulting.com/rif/hello-world-2/ When
Problem with Maven Dependency. Hello All, This is in reference to my earlier post

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.