Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
334 views
in Technique[技术] by (71.8m points)

c++ - 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;
}
question from:https://stackoverflow.com/questions/65894320/could-someone-explain-to-me-what-uint64-t-is-doing-exactly

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...