0

I'm trying to implement a function in Solidity that checks if the bit at a position is set or not, i.e. I want a function like:

function isBitSet(bytes24 b, uint pos) returns (bool){
    return ...
}

I tried to transfer this approach to Solidity: Checking if a bit is set or not

function isBitSet(bytes24 b, uint pos) returns (bool){
    return (b & (1 << pos)) != 0;
}

However, this does not work because (1 << pos) returns type uint256...

David S.
  • 31
  • 2

1 Answers1

0
function isBitSet(bytes24 b, uint pos) internal view returns (bool){
    return (  bytes32(b) & bytes32(1 << (pos+64)) ) != 0;
}
Mad Jackal
  • 1,219
  • 1
  • 7
  • 9
  • While this code may solve the question, [including an explanation](//meta.stackexchange.com/q/114762) of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please [edit] your answer to add explanations and give an indication of what limitations and assumptions apply. [From Review](/review/low-quality-posts/27780780) – double-beep Dec 18 '20 at 06:47