On 7/24/2015 12:45 PM, Paul wrote:
> My code to extract the final 8 bits of a 64 bit integer is below:
Which ones are "the final"? Lower or upper ones?
> I can't believe there isn't something more elegant and concise, perhaps by using bitset? Any ideas? Or is the below just fine?
>
> Thanks,
>
> Paul
>
> const int bitShift = 8;
> auto extractFinalBits = [bitShift](uint64_t y)->uint8_t{int result = 0; for(int i = 0; i < bitShift; ++i) if( (1 << i ) & y) result += (1 << i); return result;};
>
Do you think it's possible for you to wrap your lines and indent so the
code is actually readable?
And what is your test? To post a question on code (whether it works or
doesn't) you ought to post a complete (preferably compilable) program...
Back to your question, doesn't shift to the right 56 bits, then bitwise
AND with 0xff, work?
uint8_t res = uint8_t((y >> 56) & 0xff);
maybe?
V
--
I do not respond to top-posted replies, please don't ask