- 01
- 02
- 03
- 04
- 05
- 06
- 07
- 08
- 09
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
* Calculates the minimum number of bits necessary to represent the given number. The
* number should be given in its unsigned form with the starting bits equal to 1 if it is
* signed. Repeatedly compares number to another unsigned int called x.
* x is initialized to 1. The value of x is shifted left i times until x is greater
* than number. Now i is equal to the number of bits the UNSIGNED value of number needs.
* The signed value will need one more bit for the sign so i+1 is returned if the number
* is signed, and i is returned if the number is unsigned.
* @param number the number to compute the size of
* @param bits 1 if number is signed, 0 if unsigned
*/
public static int minBits(int number, int bits)
{
int val = 1;
for (int x = 1; val <= number && !(bits > 32); x <<= 1)
{
val = val | x;
bits++;
}
if (bits > 32)
{
assert false : ("minBits " + bits + " must not exceed 32");
}
return bits;
}
Адоб, как обычно, порадовал (условие окончания цикла).
[color=blue]https://git-wip-us.apache.org/repos/asf/flex-sdk/repo?p=flex-sdk.git;a=blob;f=modules/swfutils/src/java/flash/swf/SwfEncoder.java;h=03a100dda92989d537b00b 96033d614c73c47801;hb=HEAD#l320[/code]
wvxvw 17.08.2014 16:39 # 0
О, а еще умело использованый ассерт: только сейчас дошло...
kegdan 17.08.2014 17:11 # 0
kegdan 17.08.2014 17:18 # 0
wvxvw 17.08.2014 17:31 # 0
Но это оказалось не по силам народным зодчим. Ну и вообще-то компилятор как-бы перевели на несклолько языков, а конкретно это место решили оставить как есть...
kegdan 17.08.2014 17:39 # 0
wvxvw 17.08.2014 17:56 # 0
Qwertiy 18.08.2014 00:18 # 0
А использование bits весёленькое :D
3.14159265 22.08.2014 19:20 # +1