Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11
  1. #1
    Regular Coder
    Join Date
    Jun 2002
    Posts
    349
    Thanks
    2
    Thanked 0 Times in 0 Posts

    objects and how to use bitwise and comparisions

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

  • #2
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    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?
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #3
    Regular Coder
    Join Date
    Jun 2002
    Posts
    349
    Thanks
    2
    Thanked 0 Times in 0 Posts
    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...

  • #4
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    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:

    Code:
    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:
    Code:
    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:
    Code:
    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	128
    So, 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):
    Code:
    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	59
    Here, what is happening?

    Well, let's break one of those comparisons down:
    Code:
    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.
    Last edited by liorean; 05-29-2004 at 03:37 AM.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #5
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    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.)
    Code:
    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	255
    Here 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:
    Code:
    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	255
    Well, same here, the pattern may be hard to discern. Let's make it much clearer at once, however, by looking at the binaries:
    Code:
    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 <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #6
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    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.)
    Code:
    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	127
    Here 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...
    Code:
    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	196
    Again the same pattern. Explaining why is easiest done by looking at the binaries, however:
    Code:
    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.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #7
    Senior Coder
    Join Date
    Jul 2003
    Location
    My pimped-out igloo in Canadia
    Posts
    1,966
    Thanks
    36
    Thanked 0 Times in 0 Posts
    i'm impressed
    Before you criticize someone, you should walk a mile in their shoes. That way, when you criticize them, you're a mile away and you have their shoes :)

  • #8
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    Really? Impressed by empirical testing and conclusions that only sum up what the reference 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...)
    Last edited by liorean; 05-29-2004 at 03:11 AM.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #9
    Senior Coder
    Join Date
    Jun 2002
    Location
    near Oswestry
    Posts
    4,508
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I'm impressed by the fact you understand all this ...
    "Why bother with accessibility? ... Because deep down you know that the web is attractive to people who aren't exactly like you." - Joe Clark

  • #10
    Senior Coder
    Join Date
    Jul 2003
    Location
    My pimped-out igloo in Canadia
    Posts
    1,966
    Thanks
    36
    Thanked 0 Times in 0 Posts
    heh, okay, so i'm easily impressed

    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


    hehehe -- Cheers,
    -- Jay
    Last edited by canadianjameson; 05-30-2004 at 07:22 PM.
    Before you criticize someone, you should walk a mile in their shoes. That way, when you criticize them, you're a mile away and you have their shoes :)

  • #11
    Regular Coder
    Join Date
    Jun 2002
    Posts
    349
    Thanks
    2
    Thanked 0 Times in 0 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •