Created
March 29, 2020 15:11
-
-
Save gakonst/c9ffbbf181275cf68eb0d3f21556f902 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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