Bitwise operators in JS

Okay, so I read at the above link and elsewhere that in normal JavaScript that bitwise operators aren’t the performance boosters they are in other languages.

I understand that there are significant differences between the compiled JavaScript that Unity uses and standard JavaScript, but I need to be sure:

In Unity, can I use bitwise operators in JavaScript for the significant speed increase I would expect to see in C (etc)?

In general, any information on even minimal speed increases using bitwise operations in Unity (in JS, C#, or Boo) would be greatly appreciated.

PS: Yes, I actually need to optimize this part of my code, and no, there is no way to get around it.

Humble thanks,
Chris P.

It depends on what you’re doing…just using “foo *= 2” is actually a bit faster than “foo <<= 1”, for example. “foo *= 8” and “foo <<= 3”, on the other hand, are the same. The code is already optimized behind the scenes in some way, but since it’s managed code you obviously don’t have the control that you’d have in C. The only way to tell what’s effective is by running benchmarks.

–Eric

Can you give a bit of detail about what you are doing and where you need to optimise? These days even a C compiler carries out these “reduction in strength” optimisations for simple arithmetic, so I’m guessing you are using bitwise operators for something more complicated than that.

Certainly. I am developing a puzzle game. It has four Euclidean spatial dimensions. Game play is based around a unique set of rules I developed. It is a somewhat unique version of the Constraint Satisfaction Problem. See Constraint satisfaction problem - Wikipedia.

The problem I am attempting to resolve is a fear that procedural creation of a puzzle is unrealistic on lower performance systems (such as the iPhone, my original target market). I unfortunately do not have access to an iPhone, and have tenuous (at best) access to Unity itself. Therefor I have not yet run benchmarks.

What I feel I need to most focus on optimizing are these problems:

Given an input bitstring, and a blank bitstring (all bits are FALSE), place one random bit form the input bitstring into the blank bitstring.

Puzzle pieces are a hypercube of one unit magnitude. They are arranged in a regular and compact manner. Create a list of all of the values along one surface or hypersurface.

I am already aware of Hamming distance function and will be employing it. Notes on its usage would be appreciated.

Many of these calculations will need to be either performed repeatedly or stored by the individual game pieces.

I couldn’t sleep last night, so I worked on the logic used to set the puzzle. I believe I have it so that I can attempt a nearly entirely systematic (instead of probalistic) approach to setting the puzzle, and that this should scale better as the magnitude of dimensions increases.

However, I believe it may be necessary to update a large number of abstract, behind-the-scenes sets of bitstrings in order to access them readily and consider if the constraints are being met. Each of these bitstrings represent the puzzle pieces along a hypersurface of game pieces, though this hypersurface may only be 1 unit thick (in up to 3 dimensions. ie, a line).

I am looking at up to ~1000 such structures. ~90% of these will contain only 10 puzzle pieces, and the remainder would contain about 50 puzzle pieces. There are a total of ~750 such puzzle pieces, and each structure would have to be updated when one of its member pieces was updated. Puzzle piece representation in structures should be not-quite but nearly uniform.

[[ Those numbers are only nearly correct if I can trust myself to think in four dimensions with a cold… ]]

I am not yet certain if iterating over the puzzle pieces each time they need to be checked for constraint satisfaction is superior to trying to ‘remember’ them (and needing to update all the memories during each state change).

Forgive me for remaining somewhat opaque, but the rest of my development team (who are all MIA in regards to actual development) would shit bricks if they found out I shared the specific game play idea I have come up with. I will gladly answer any questions as best as I can.

Also, if I seem to have any gaps in my knowledge, please enlighten me. I have no formal training in programming and precious little in mathematics (all of what they taught me in school is useless here, lol).

Kind regards,
Chris P.

PS: I can use up to 32 bits when using bitwise operators, correct? What if I program for a 64 bit architecture in mind? I have no idea how that side of programming works, please let me know if/how its possible to expand beyond the 32 bit limitation of bitwise operators.

No worries; I haven’t the vaguest idea what you’re talking about, and therefore will not be stealing this idea and can offer no advice. :slight_smile:

If you use a 32-bit datatype (int), then you’re limited to 32 bits. If you use a 64-bit datatype (long), then you can use 64 bits.

–Eric

My bad! I should have writ with a more specific focus.

There are two problems I want to optimize, the rest is just logic (I can figure it out, will take a bit of experimentation).

#1
Given an input bitstring, and a blank bitstring (all bits are FALSE), place one random bit from the input bitstring into the blank bitstring.

#2
Imagine a 4 dimensional array of unsigned long integers. Each element in the array has one of 64 values. This means that each element in the array has a single TRUE bit (they are stored as powers of two).

From this array, determine which other elements share exactly one index (a constraint). Of those elements, determine which have at least two of the remaining indexes within a certain range (variable constraints). Of those elements, generate a bitstring by using OR on each of them. (initial bitstring state is all bits FALSE)

Those are my main hurdles.

The logic comes from determining if I should iterate over the array every time I need to check constraints (in problem #2), or creating an abstract ‘memory’ of how the array looks from different constraint sets… but that is something I can figure out on my own once I know how to efficiently do the above.