HTML and CSS Reference
In-Depth Information
nutshell, bitfields are used to pack a lot of boolean values into the bits of the boolean
representation of a number. For example, if you have eight values (true, false, false, true,
false, true, true, false), the number would be 10010110 in binary; so, 150 in decimal and 96 in
hex. That's 2 characters instead of 8, so 75% saved . In general, 1 digit in the hex
representation corresponds to exactly 4 bits. (That's because 16=2 4 . In general, in a base2 n
system, you can pack n bits into every base2 n digit.) So, we weren't lucky with that 75%; it's
always that much .
Thus, instead of storing that string as a string and using 1 character per value, we can be
smarter and convert it to a (hex) number first. How do we do that? It's no more than a line of
parseInt ( '10010110' , 2 ). toString ( 16 ); // returns '96'
And how do we read it back? That's just as simple:
parseInt ( '96' , 16 ). toString ( 2 ); // returns '10010110'
From this point on, we can follow the same process as the previous method to loop over the
values and do something useful with them.
Can We Do Better?
In fact, we can! Why convert it to a hex (base 16) number, which uses only 6 of the 26
alphabet letters? The Number#toString() method allows us to go up to base 36 (throwing a
RangeError for >=37 ), which effectively uses all letters in the alphabet, all the way up to z!
This way, we can have a compression of up to 6 characters for 32 values, which means saving
up to 81.25% compared to the plain string method! And the code is just as simple:
parseInt ( '1001011000' , 2 ). toString ( 36 ); // returns 'go' (instead of '258',
which would be the hex version)
parseInt ( 'go' , 36 ). toString ( 2 ); // returns '1001011000'
For some of you, this will be enough. But I can almost hear the more inquisitive minds out
there shouting, “But we have capital letters, we have other symbols, we are still not using
strings to their full potential!” And you'd be right. There is a reason why every time you open a
binary file in a text editor, you get weird symbols mixed with numbers, uppercase letters,
lowercase letters and whatnot. Every character in an UTF-16 string is a 2 bytes (16 bits),
Search WWH ::

Custom Search