Skip to content

Instantly share code, notes, and snippets.

@gakonst
Created March 29, 2020 15:11
Show Gist options
  • Save gakonst/c9ffbbf181275cf68eb0d3f21556f902 to your computer and use it in GitHub Desktop.
Save gakonst/c9ffbbf181275cf68eb0d3f21556f902 to your computer and use it in GitHub Desktop.
pragma solidity ^0.6.2;
contract Test {
function test1() external {
numOfPeaks(200);
}
function test2() external {
numOfPeaks2(200);
}
function numOfPeaks(uint256 width) public pure returns (uint256 num) {
uint256 bits = width;
while (bits > 0) {
if (bits % 2 == 1) num++;
bits = bits >> 1;
}
return num;
}
function numOfPeaks2(uint256 width) public pure returns (uint256 num) {
uint256 bits = width;
while (bits > 0) {
if (!(bits % 2 == 0)) num++;
bits = bits >> 1;
}
return num;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment