Skip to main content

News

Topic: Bitwise operator not working as expected. (Read 6961 times) previous topic - next topic

0 Members and 1 Guest are viewing this topic.
  • Mooch
  • [*][*][*]
Bitwise operator not working as expected.
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.

Code: [Select]
var font = GetSystemFont();
var blah = 12;
var sup = blah & 12;

while (!IsKeyPressed(KEY_ESCAPE)) {
        font.drawText(0, 0, sup);
        FlipScreen();
}


According to...

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Bitwise_Operators?redirectlocale=en-US&redirectslug=JavaScript%2FReference%2FOperators%2FBitwise_Operators

...the bitwise operation "&" should be returning a boolean value, right? But the code just returns a 12. Even if I change it to this...

Code: [Select]
var font = GetSystemFont();
var blah = 12;
var sup = blah & 23647;

while (!IsKeyPressed(KEY_ESCAPE)) {
        font.drawText(0, 0, sup);
        FlipScreen();
}


The code still just displays the number 12 on the screen.

Any idea...
A) Why the code displays 12 no matter what you & with blah in sup?
B) Why blah & 12 doesn't return a 1 in the first place?

  • Fat Cerberus
  • [*][*][*][*][*]
  • Global Moderator
  • Sphere Developer
Re: Bitwise operator not working as expected.
Reply #1
Boolean !== bitwise.  One is logical, dealing only in true or false (nonzero/zero), the latter manipulates individual binary bits.

Not positive, but I think what you want is the && operator - logical AND.
neoSphere 5.9.2 - neoSphere engine - Cell compiler - SSj debugger
forum thread | on GitHub

  • Mooch
  • [*][*][*]
Re: Bitwise operator not working as expected.
Reply #2
Tried that. It just prints the second number in sup.

Okay, weirdness. I tried...
var blah = 0100;
var sup = blah & 0010;
...and it printed "0."

Then...
var blah = 0100;
var sup = blah & 0100;
...returns "64."

And...
var blah = 0100;
var sup = blah && 0010;
...returns 8!

I clearly have no idea what's going on here. What I want is to be able to and-compare to see if a given bit in a byte is a one or not.

Like, if I had...
whatevs = 0000 0010

I want to be able to do something like...
if whatevs & 0000 0010 = 1 then move player right
else if whatevs & 0000 0010 = 0 then do nothing

Re: Bitwise operator not working as expected.
Reply #3

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 )
  • Last Edit: December 08, 2013, 04:08:45 am by Radnen

  • Mooch
  • [*][*][*]
Re: Bitwise operator not working as expected.
Reply #4
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.

  • DaVince
  • [*][*][*][*][*]
  • Administrator
  • Used Sphere for, like, half my life

Re: Bitwise operator not working as expected.
Reply #6
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 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.

  • Fat Cerberus
  • [*][*][*][*][*]
  • Global Moderator
  • Sphere Developer
Re: Bitwise operator not working as expected.
Reply #7
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?
neoSphere 5.9.2 - neoSphere engine - Cell compiler - SSj debugger
forum thread | on GitHub

  • Mooch
  • [*][*][*]
Re: Bitwise operator not working as expected.
Reply #8

This might help: http://stackoverflow.com/questions/2803145/is-there-0b-or-something-similar-to-represent-a-binary-number-in-javascript


Thanks.


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 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.

Re: Bitwise operator not working as expected.
Reply #9

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.

Code: [Select]

function GetBit(value, n){
  return value&(1<<n);
}

  • N E O
  • [*][*][*][*][*]
  • Administrator
  • Senior Administrator
Re: Bitwise operator not working as expected.
Reply #10
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.