var font = GetSystemFont();var blah = 12;var sup = blah & 12; while (!IsKeyPressed(KEY_ESCAPE)) { font.drawText(0, 0, sup); FlipScreen();}
var font = GetSystemFont();var blah = 12;var sup = blah & 23647; while (!IsKeyPressed(KEY_ESCAPE)) { font.drawText(0, 0, sup); FlipScreen();}
Then...var blah = 0100;var sup = blah & 0100;...returns "64."
var num = 0xFF;//same as 1111 1111 binary, and 255 in decimal.//test the third bitvar mask = 0x04;//same as 0000 0100 binary, note the third bit is a 1 and all others are 0's.if(mask&num){ //do things!}
This might help: http://stackoverflow.com/questions/2803145/is-there-0b-or-something-similar-to-represent-a-binary-number-in-javascript
This isn't quite what you are asking for, but using Hexadecimal literals is how it's often done (even in low level languages, like C++ and C). Hexadecimal is pretty handy for representing binary. It has the advantage over parseInt() that it can be written as literals (just the number, no function calls), and the disadvantage that it is not, in fact, binary.To write hex, you put a leading '0x' in front. Like this:Code: [Select]var num = 0xFF;//same as 1111 1111 binary, and 255 in decimal.//test the third bitvar mask = 0x04;//same as 0000 0100 binary, note the third bit is a 1 and all others are 0's.if(mask&num){ //do things!}The calculators on most OS's (and DE's in teh Linuxes) can convert binary, decimal, and hexadecimal for you. The advantage of Hex over decimal for representing a binary number is that each digit in hex is four digits in binary, so it lines up well (very much unlike decimal, sort of unlike octal). Plus, you only have to memorize 16 digits of hex to know the representation in binary without any help from calculators or converters.
Yeah, the beauty of hexadecimal is that a single hex digit is exactly one nibble, two is a full byte. It gives you a nice, compact representation of raw binary data, this is why hex editors are used to edit binary files. On a sidenote, what is the purpose of octal? Who uses it? Base 8 only gives you 3 bits per digit, so it's kind of clunky to use for bitfields or anything (as 3 doesn't go into 8, 16, 32 or 64 evenly). Is there an use case for it that I'm not thinking of?
How would I return a boolean that returns 0 if there's a 0 at the specified bit and 1 if there's a 1? In Javascript. I know how to do it in theory, just not specifically in JS.
function GetBit(value, n){ return value&(1<<n);}