Skip to content

Instantly share code, notes, and snippets.

@FaAway
Created March 19, 2016 12:56
Show Gist options
  • Save FaAway/432cec2e8f08420052b8 to your computer and use it in GitHub Desktop.
Save FaAway/432cec2e8f08420052b8 to your computer and use it in GitHub Desktop.
javarush level30.lesson08.task01
package com.javarush.test.level30.lesson08.task01;
/* Найдем число 2 в максимальной степени
Реализуйте логику метода maxPowerOf2,
который должен возвращать число 2 в максимальной степени, которое получается поместить в переданное число
Аргументом maxPowerOf2 может быть только положительное число
Используйте только операции: 1)побитовые сдвиги, 2) присваивание, 3) побитовое ИЛИ
Не оставляйте комментарии
*/
public class Solution {
public static void main(String[] args) {
System.out.println(maxPowerOf2(140_000)); //131072
System.out.println(maxPowerOf2(1026)); //1024
System.out.println(maxPowerOf2(17)); //16
}
public static int maxPowerOf2(int x) {
x |= x >> 1;
x |= x >> 2;
x |= x >> 4;
x |= x >> 8;
x |= x >> 16;
return x - (x >> 1);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment