Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2017
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.69 KB | None | 0 0
  1. public class MathPow
  2. {
  3. public int largestPowerOf2 (int n)
  4. {
  5. int res = 2;
  6. while (res < n) {
  7. res =(int)Math.pow(res, 2);
  8. }
  9.  
  10. return res;
  11. }
  12. }
  13.  
  14. Arguments Actual Expected
  15. -------------------------
  16. 9 16 8
  17. 100 256 64
  18. 1000 65536 512
  19. 64 256 32
  20.  
  21. Integer.highestOneBit(n-1);
  22.  
  23. v--;
  24. v |= v >> 1;
  25. v |= v >> 2;
  26. v |= v >> 4;
  27. v |= v >> 8;
  28. v |= v >> 16;
  29. v++;
  30. v >>= 1;
  31.  
  32. public int largestPowerOf2(int n) {
  33. return (int)Math.pow(2, Math.floor(Math.log(n) / Math.log(2));
  34. }
  35.  
  36. 0x80000000 >>> Integer.numberOfLeadingZeros(n - 1);
  37.  
  38. public class MathPow{
  39. public int largestPowerOf2(int n){
  40. if((n & n-1) == 0){ //this checks if n is a power of 2
  41. n--; //Since n is a power of 2 we have to subtract 1
  42. }
  43. while((n & n-1) != 0){ //the while will keep on going until n is a power of 2, in which case n will only have 1 bit on which is the maximum power of 2 less than n. You could eliminate the != 0 but just for clarity I left it in
  44. n = n & n-1; //we will then perform the bitwise operation AND with n and n-1 to eliminate the least significant bit of n
  45. }
  46. return n;
  47. }
  48. }
  49.  
  50. 10101000
  51. & 10100111
  52. ------------
  53. 10100000
  54.  
  55. 10100000
  56. & 10011111
  57. -------------
  58. 10000000
  59.  
  60. 1111
  61. & 1110
  62. --------
  63. 1110
  64.  
  65. 1110
  66. & 1101
  67. ---------
  68. 1100
  69.  
  70. 1100
  71. & 1011
  72. --------
  73. 1000
  74.  
  75. public class MathPow
  76. {
  77. public int largestPowerOf2 (int n)
  78. {
  79. int res = 2;
  80. while (res < n) {
  81. res =res*2;
  82. }
  83.  
  84. return res;
  85. }
  86. }
  87.  
  88. int res = 2;
  89. while (res * 2 < n) {
  90. res *= 2;
  91. }
  92.  
  93. public static int nextPowDown(int x, int z) {
  94. if (x == 1)
  95. return z;
  96. return nextPowDown(x >> 1, z << 1);
  97. }
  98.  
  99. public static int nextPowTailRec(int x) {
  100. return x <= 2 ? x : nextPowDown(x >> 1) << 1;
  101. }
  102.  
  103. System.out.println(nextPowDown(60, 1)); // prints 32
  104. System.out.println(nextPowDown(24412, 1)); // prints 16384
  105. System.out.println(nextPowDown(Integer.MAX_VALUE, 1)); // prints 1073741824
  106.  
  107. public class MathPow
  108. {
  109. public int largestPowerOf2(int n)
  110. {
  111. int res = 1;
  112. while (res <= (n-1)/2)
  113. {
  114. res = res * 2;
  115. }
  116.  
  117. return res;
  118. }
  119. }
  120.  
  121. p=2;
  122. while(p<=n)
  123. {
  124. p=2*p;
  125. }
  126. p=p/2;
  127.  
  128. BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  129.  
  130. String num = br.readLine();
  131. BigInteger in = new BigInteger(num);
  132. String temp = in.toString(2);
  133.  
  134. System.out.println(new BigInteger("2").shiftLeft(temp.length() - 2));
  135.  
  136. BigInteger nvalue = TWO.pow(BigIntegerMath.log2(value, RoundingMode.FLOOR));
  137.  
  138. n = (x>>>0).toString(2).length-1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement