...

View Full Version : objects and how to use bitwise and comparisions



BrightNail
05-28-2004, 08:06 PM
hello,
I am need of some GREAT tutorials on how to create objects for testing items that will be comparisions based on bitwise --- any help or ideas....

liorean
05-28-2004, 08:32 PM
There's really not much point to using the bitwise operators in JavaScript except in some very special situations. Do you think you could elaborate a bit on what you more precisely want to do?

BrightNail
05-28-2004, 11:12 PM
my goal is that I am gonna be returned some objects from mod_perl....the elements and such will all be related in some way or another...I need to use bitwise to these associations and such....so, I was wondering how best to go about this....tutorials and such...

liorean
05-29-2004, 12:01 AM
I don't think you have much tutorials on the subject, really. They're not commonly used in JavaScript.

Can you give more detail, about the object structure returned? Then I might go about explaining how you might use thm for you. However, here's a few examples:


Test values:
0 "00000000"
1 "00000001"
2 "00000010"
3 "00000011"
4 "00000100"
7 "00000111"
12 "00001100"
29 "00011011"
255 "11111111"

Bit comparisons:
"00000001" 1
"00000010" 2
"00000100" 4
"00001000" 8
"00010000" 16
"00100000" 32
"01000000" 64
"10000000" 128

Okay, then we can test for whether a field has a certain bit by doing the following:
var
aTests=[0,1,2,3,4,7,12,29,255],
aBits=[1,2,4,8,16,32,64,128],
aResults=[],
i=aTests.length,
j;
while(i-->0){
aResults[?i]=[];
j=aBits.length;
while(j-->0){
aResults[?i][j]=aTests[?i]&aBits[j];
}
aResults[?i]=aResults[?i].join(' ');
}
aResults.join('\n');
As a result, you get the following matrix:
0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0
0 2 0 0 0 0 0 0
1 2 0 0 0 0 0 0
0 0 4 0 0 0 0 0
1 2 4 0 0 0 0 0
0 0 4 8 0 0 0 0
1 0 4 8 16 0 0 0
1 2 4 8 16 32 64 128So, you see that in cases where it matches, the return result of that comparison is exatly the same bits as you compared. Now, all those were single bit comparisons. If you bring them into pairs, you'll get something that is more interesting. Run that same code with aBits set to [3,7,59] (binary "00000011", "00000111", "00111011" respectively):
0 0 0
1 1 1
2 2 2
3 3 3
0 4 0
3 7 3
0 4 8
1 5 25
3 7 59Here, what is happening?

Well, let's break one of those comparisons down:
00001100 12 (Number to compare)
00111011 59 (Number compared to)
---------
00001000 8 (Number returned)The result, in other words, is the sum of all bits where both the compared and the comparee number have the bit set. So, in the case of a bitwise AND on 12 and 59, the single bit they have in common is the bit representing the number 8, which is the return value. The return values of bitwise AND could be said to be the union, or common positives, of the set of bits in both numbers.

As for practical use, I can see some use for it in single bit comparisons, but seldomly for multiple bit comparisons, unless what you want to check is what bits are common to two numbers.

liorean
05-29-2004, 01:33 AM
Okay, next example. We're changing aResults[​i][j]=aTests[​i]&aBits[j]; into aResults[​i][j]=aTests[​i]|aBits[j]; and repeating both tests. (Bitwise AND changed into bitwise OR.)
1 2 4 8 16 32 64 128
1 3 5 9 17 33 65 129
3 2 6 10 18 34 66 130
3 3 7 11 19 35 67 131
5 6 4 12 20 36 68 132
7 7 7 15 23 39 71 135
13 14 12 12 28 44 76 140
29 31 29 29 29 61 93 157
255 255 255 255 255 255 255 255Here the result may be a bit harder to read out from the results of the first matrix. To make realising what happens here easier, we'll have look at the binaries. However, first the multiple bits comparisons:
3 7 59
3 7 59
3 7 59
3 7 59
7 7 63
7 7 63
15 15 63
31 31 63
255 255 255Well, same here, the pattern may be hard to discern. Let's make it much clearer at once, however, by looking at the binaries:
00001100 12 (Number to compare)
00000111 7 (Number compared to)
---------
00001111 15 (Number returned)There, it's suddenly so much clearer! It returns the set of bits that are set in either just one of the numbers, or that are set in both of the numbers. In other words, you can here find out the total set of bits that are set on the two numbers.

liorean
05-29-2004, 01:43 AM
Okay, next example. We're changing aResults[​i][j]=aTests[​i]|aBits[j]; into aResults[​i][j]=aTests[​i]^aBits[j]; and repeating both tests. (Bitwise OR changed into bitwise XOR.)
1 2 4 8 16 32 64 128
0 3 5 9 17 33 65 129
3 0 6 10 18 34 66 130
2 1 7 11 19 35 67 131
5 6 0 12 20 36 68 132
6 5 3 15 23 39 71 135
13 14 8 4 28 44 76 140
28 31 25 21 13 61 93 157
254 253 251 247 239 223 191 127Here we can see an interesting pattern. A diagonal of zeroes in the first matrix, where we get a zero at any time the comparee and the compared are equal...
3 7 59
2 6 58
1 5 57
0 4 56
7 3 63
4 0 60
15 11 55
30 26 38
252 248 196Again the same pattern. Explaining why is easiest done by looking at the binaries, however:
00001100 12 (Number to compare)
00000111 7 (Number compared to)
---------
00001011 11 (Number returned)Again, it's suddenly so much clearer. It returns the set of bits that are set in just one of the numbersbut not in both of the numbers. In other words, you can here find out the total set of bits that are set on just one of the nubmers, but not the other.

canadianjameson
05-29-2004, 02:00 AM
i'm impressed

liorean
05-29-2004, 02:04 AM
Really? Impressed by empirical testing and conclusions that only sum up what the reference (http://devedge.netscape.com/library/manuals/2000/javascript/1.5/reference/ops.html#1042500) is saying?

In fact, I could have linked there from the start. I believe that seeing real code being used to deduce a solution is often more effective than just linking to the explanation, however.

(I also believe you learn better if the tutorial is spelled correctly. Oh, well, what can I say - it's three in the morning...)

brothercake
05-29-2004, 02:21 AM
I'm impressed by the fact you understand all this ...

canadianjameson
05-29-2004, 02:28 AM
heh, okay, so i'm easily impressed :D

hehe, just kidding, get some sleep -- you'll need it when the six Swedish models i hired get to your place to thank you for helping me with my scripts/pages.

:)

and they'll all be wearing these:
URL removed - remember this a family forum
- my bad brothercake, i wasn't quite sure whether it would pass or not :o


hehehe -- Cheers,
-- Jay :)

BrightNail
05-29-2004, 07:09 AM
wow..thank you for your examples...I am gonna go thru them, and then formulate my next question in a better way so you can understand what I'm driving at...again, thanks - really appreciate it.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum