View Full Version : Quick question on bit arrays
Algorithm
10-20-2002, 03:43 AM
I'm creating a class that employs an array of bits. Would there be any signifigant advantage to storing them as boolean literals instead of integer values?
beetle
10-20-2002, 04:01 AM
Uhhh...if you only need a boolean true/false or bitwise 1/0 it doesn't matter much. A 1|0 integer will evaluate as true/false in a conditional, and require less typing. And, realistically, your array would have to be pretty big for you to notice any type of 'performance difference'
If you are storing an array of bits, why not just use an integer?
var bytearray = parseInt('0111000111', 2);
bytearray == 455;
You can set the nth bit from the right to true by:
bytearray |= Math.pow(2, n)
Algorithm
10-20-2002, 04:58 AM
beetle: It could very well be a big array, so performance counts. I wouldn't ask otherwise.
jkd: I'm going to be using this in a queue-like fashion, so it has to be an array. No shortcuts.
beetle
10-20-2002, 05:48 AM
I suppose you'll just have to run some tests then. Let us know how it comes out :D
RadarBob
10-20-2002, 05:28 PM
As I understand it one uses bits & bit-shifting for performance purposes. Due to both the nature and construction of digital computers, bit shifting is faster than "conventional" arithmetic or logic programming code.
To use bit shifting to substitue for built-in JavaScript features just makes the code so much hyrogliphics and serves no practical purpose given the abstract level we program at.
With bit-shifting, we're talking advantages down at the assembler/machine language / register level of the CPU. This esoteric area of programming is primarily of interest to systems programmers - those who build operating systems, compilers, etc. and who's code has to execute bazillions of times all day long.
Given today's computer speeds, quality hi-level languages, optimizing compilers, abundant computer memory & storage, etc. bit manipulation is not as important as it was 30 years ago.
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.