Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- bool isPowerTwo(int number) {
- bool hasOne = false;
- unsigned int current = number;
- while (true) {
- /* If the number was 0, then hasOne is false. Otherwise,
- * hasOne is true, but since we are here, the number had only
- * one bit as 1. Thus the number is a power of two.
- */
- if (current == 0) return hasOne;
- if ((current & 1) == 1) {
- /* If a number has more than one bit as 1, then it is not
- * a power of two.
- */
- if (hasOne) return false;
- hasOne = true;
- }
- // Remove a bit.
- current = current >> 1;
- }
- }
- vector <int> isPowerOf2(vector <int> nums) {
- vector <int> answers = {};
- for (int i = 0; i < nums.size(); i++) {
- answers.push_back(isPowerTwo(nums[i]));
- }
- return answers;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement