Sphere Development => Sphere Support => Topic started by: Mooch on December 07, 2013, 10:16:59 am
Title: Bitwise operator not working as expected.
Post by: Mooch on December 07, 2013, 10:16:59 am
As I think I've proven, I'm no old hat at programming in general or Javascript specifically. I can't figure out why this bitwise operation isn't working.
Then... var blah = 0100; var sup = blah & 0100; ...returns "64."
0100 is octal 100, which is hex 40, decimal 64. Decimal 64 bitwise-AND decimal 64 = decimal 64. A leading zero indicates octal (Base 8 ) in JS.
Bitwise operators operate on bits, binary.
radnen edit: fixed, 8) confuses with 8 )
Title: Re: Bitwise operator not working as expected.
Post by: Mooch on December 09, 2013, 06:42:23 am
Whoops! I thought I was doing binary. How do you do binary in JS? I can't find anything on google, and simply having eight bits doesn't work, it still reads it as octal.
Title: Re: Bitwise operator not working as expected.
Post by: DaVince on December 09, 2013, 08:06:06 am
This might help: http://stackoverflow.com/questions/2803145/is-there-0b-or-something-similar-to-represent-a-binary-number-in-javascript
Title: Re: Bitwise operator not working as expected.
Post by: Flying Jester on December 09, 2013, 08:44:15 am
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:
var num = 0xFF; //same as 1111 1111 binary, and 255 in decimal.
//test the third bit var 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.
Title: Re: Bitwise operator not working as expected.
Post by: Fat Cerberus on December 09, 2013, 09:54:41 am
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?
Title: Re: Bitwise operator not working as expected.
Post by: Mooch on December 09, 2013, 10:28:52 am
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:
var num = 0xFF; //same as 1111 1111 binary, and 255 in decimal.
//test the third bit var 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.
Heyo! It works! num&mask prints 4, which is obviously the value left when you & the two values together.
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.
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?
UNIX file permissions use octal.
Title: Re: Bitwise operator not working as expected.
Post by: Flying Jester on December 09, 2013, 02:25:19 pm
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.
Well, if you don't want a bunch of hexadecimal literals, you could use bitshifting.
Title: Re: Bitwise operator not working as expected.
Post by: N E O on December 09, 2013, 03:38:25 pm
Actually, I found out the hard way that in JS you need to coerce the type after performing the bitwise AND. It's just easier, therefore to test "if ((val&mask)>0)" to get the result you want.