FatPanda
FatPanda

Reputation: 13

Could someone explain to me what uint64_t is doing exactly?

I am trying to undertand how this code works, specifically the line msk = ~(uint64_t) 0 << (uint64_t)(high - low + 1);

I really only am curious what uint64_t is calling/doing. Not so much the bit operations

Thanks for any insight!

uint64_t clearBits(unsigned low, unsigned high, uint64_t source){
    uint64_t msk;
    assert(high < 64 && (low <= high));
    msk = ~(uint64_t) 0 << (uint64_t)(high - low + 1);
    return source & msk;
}

Upvotes: 1

Views: 12174

Answers (1)

Code-Apprentice
Code-Apprentice

Reputation: 83557

uint64_t is an integer type. uint64_t msk; declares a variable of that type and (uint64_t) 0 casts 0 to that type since the literal 0 is an int. Similarly, (uint64_t)(high - low + 1) casts the result of a calculation to the type uint64_t. However, this second cast is unnecessary, as discussed in the comments since the type of the result of << only depends on the type of the first operand and not the second.

Upvotes: 4

Related Questions