Solidity: Checking if specific bit is set in bytes24

455 Views Asked by At

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...

1

There are 1 best solutions below

1
On
function isBitSet(bytes24 b, uint pos) internal view returns (bool){
    return (  bytes32(b) & bytes32(1 << (pos+64)) ) != 0;
}