I need Java code that can find the least power of 2, greater than or equal to any non-negative integer entered by the user. Can anyone help?
i>1 ? Integer.highestOneBit(i-1)<<1 : 1
Obviously, it suffers from integer overflow (a strict solution in intdoes not exist strictly for half positive ints).
int
Regular disclaimer: not verified or compiled.
See this link
Algorithm for finding the smallest degree of two quantities greater than or equal to a given value
Bye
int nextPow2(int n) { if (n <= 1) return n; double d = n - 1; return 1 << ((Double.doubleToRawLongBits(d) >> 52) - 1022); }
, .
if (i==0) return 1; else if (i==1) return 2; else if (i==2 || i==3) return 4; else if (i==4 || i==5 || i==6 || i==7) return 8; else if (...)
you can do this by counting the number of unsigned right shifts until the result is zero
Source: https://habr.com/ru/post/1718337/More articles:InstallShield 2009 Pre install - installshieldEditText не возвращает содержимое на getText() - androidOracle Error PLS-00103. How can you check an existing record and update it or insert based on this condition? - sqldjango - using a common header with some dynamic elements - javascriptCreating CakePHP Page Using HABTM Models - phpSmooth polyline with minimal deformation - c #Django: how to enable model inline fields in list_display? - pythonWas there anything inside Cobol that made him susceptible to Y2K issues? - cobolПочему этот код таймера С# не работает? - c#Connecting all function calls to JavaScript? - javascriptAll Articles