Why does this Hackerrank Bit Array solution work?

This is one of the free Hackerrank problems online:

You are given four integers: N, S, P, Q. You will use them in order to create the sequence with the following pseudo-code:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>a[0] = S (modulo 2^31)
for i = 1 to N-1
a[i] = a[i-1]*P+Q (modulo 2^31)
</code>
<code>a[0] = S (modulo 2^31) for i = 1 to N-1 a[i] = a[i-1]*P+Q (modulo 2^31) </code>
a[0] = S (modulo 2^31)
for i = 1 to N-1
    a[i] = a[i-1]*P+Q (modulo 2^31) 

Your task is to calculate the number of distinct integers in the sequence a.

I’m trying to understand solution #2
here. It works but I can’t figure out why — specifically, the insert() function:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>unsigned long long mask[40000000];
unsigned insert(unsigned x) {
unsigned res = (mask[x >> 6] & (1ULL << (x & 0x3F))) == 0;
mask[x >> 6] |= 1ULL << (x & 0x3F);
return res;
}
</code>
<code>unsigned long long mask[40000000]; unsigned insert(unsigned x) { unsigned res = (mask[x >> 6] & (1ULL << (x & 0x3F))) == 0; mask[x >> 6] |= 1ULL << (x & 0x3F); return res; } </code>
unsigned long long mask[40000000];

unsigned insert(unsigned x) {
    unsigned res = (mask[x >> 6] & (1ULL << (x & 0x3F))) == 0;
    mask[x >> 6] |= 1ULL << (x & 0x3F);
    return res;
}

It seems the mask array is meant to contain the previous values of X, but how does it do so if it right-shifts 6 bits of X’s value away — wouldn’t that mean multiple values of X will wind up being mapped to the same mask element? Also, what’s the significance of 6 bits (including the 0x3F which is 0b111111)?

Just wondering if this is some kind of cool idiom I’ve never seen before.

There’s another StackOverflow page about this Hackerrank problem but it was a totally unrelated question.

2

Also, what’s the significance of 6 bits (including the 0x3F which is 0b111111)?

A long long, in this environment, represents a 64-bit integer. Notice that 64 is 2^6. (as an aside I would prefer to use uint64_t from #include <cstdint> to be explicit about the number of bits)

It seems the mask array is meant to contain the previous values of X, but how does it do so if it right-shifts 6 bits of X’s value away — wouldn’t that mean multiple values of X will wind up being mapped to the same mask element?

Yes, each element of mask contains 64 elements in its 64 bits.

The expression mask[x >> 6] & (1ULL << (x & 0x3F)) does two things: First, it selects an element (a long long) of the mask array by indexing using all but the least significant six bits. Second, it uses (1ULL << (x & 0x3F) to set a single bit using the remaining six bits that were ignored when indexing into the mask array. Specifically, this takes a 1, and shifts it to the position given by (x & 0x3f) – i.e. the six bits of x that weren’t used to pick an array element.

mask[x >> 6] |= 1ULL << (x & 0x3F); likewise selects an element of mask using all-but-the-last six bits, and sets a single bit within it, as selected by the last six bits of x.

Just wondering if this is some kind of cool idiom I’ve never seen before.

A single, small value with bits having meaning would be called a “bit mask” or “bit field”. In the case of an array of values holding bits, I would call it a “bit set” (e.g. std::bitset or “bit array” (as a popular Rust library calls it).

There are also dynamic ones that can grow and shrink as needed; in many cases, your C++ standard library’s std::vector<bool> will be a specialization that uses this trick for storage.

3

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật