www.webdeveloper.com
Results 1 to 9 of 9

Thread: & (ampersand) and | (pipe) with strings

  1. #1
    Join Date
    Aug 2008
    Posts
    7

    & (ampersand) and | (pipe) with strings

    Hello people.

    I have a question regarding ampersand and pipe. I've read some code where strings are parsed using the ampersand and pipe symbol, which as far as I understood were bitwise operators... Now I'm just confused because all the search queries just return things about && an || which I don't really care about.

    So do you know where I could learn more about these:

    PHP Code:
    for(a=0;a<=9;a++){
          
    document.write("_#"[a/3&1]);
    }
    //outputs: ' ___###___###___###___###___###_ ' 
    PHP Code:
    for(a=0;a<=30;a++){
         
    document.write(a/5|0);
    }
    //outputs: 0000011111222223333344444555556 
    So I've figured the effects but I'd like to understand them more in depth. Any literature I could read ?
    edit: and the role of the brackets ('[ ]') in the first code ?

    Thank you !

  2. #2
    Join Date
    Sep 2006
    Location
    Copenhagen, Denmark
    Posts
    1,253
    The two examples does not contain strings which are manipuled using & or |.
    Code:
    document.write("_#"[a/3&1]);
    This is probably referencing an array which is a property in the window object. The brackets are for array (or property) indexing.

    There is no strings in the second example.
    #define question (2B || !2B)
    HTMLElement and W3C Event Handling in IE
    My JavaScript Library

    Don't PM me about answers to questions. If I don't reply in a thread it's because:
    • You didn't read the message posting guidelines
    • Your code is too unstructured and/or formatted poorly - correcting it is too time consuming
    • I simply don't know the answer

  3. #3
    Join Date
    Aug 2008
    Posts
    7
    Okay so basically what i want to know is what does "text"[arrayindex] do ? And I'm still confused as to the use of the & and |, even with numbers.

  4. #4
    Join Date
    Jun 2003
    Location
    here
    Posts
    4,551
    Code:
    "123456"[3] == "4"
    "123456".split('')[3] == "4"
    the bold bit is just implied, I came to expect this behaviour while learning C, with it's intresting string methods :P


    as for | and &, I'm unsure what they do, as far as I can assume & reacts somewhat similar to x&y = x%(y+1), although higher numbers of y appear to work quite differently...
    If you are using PHP please use the [PHP] and [/PHP] forum tags for highlighting...
    The same applies to HTML and the forums [HTML][/HTML] tags.

  5. #5
    Join Date
    Sep 2006
    Location
    Copenhagen, Denmark
    Posts
    1,253
    Code:
    "123456"[3] == "4"
    Clever. I didn't know that.

    & is bitwise AND. This means that 1001 & 0001 = 0001 as only the first bit is set in both numbers.

    | is bitwise OR. This means that 1001 | 0001 = 1001 as the first and fourth bits are set in either number.
    #define question (2B || !2B)
    HTMLElement and W3C Event Handling in IE
    My JavaScript Library

    Don't PM me about answers to questions. If I don't reply in a thread it's because:
    • You didn't read the message posting guidelines
    • Your code is too unstructured and/or formatted poorly - correcting it is too time consuming
    • I simply don't know the answer

  6. #6
    Join Date
    Feb 2006
    Posts
    2,927
    Google 'javascript bitwise operators'-
    the Mozilla site has a good overview

    One common use on the web is converting hex color codes to decimal-

    toRgb= function(hex){
    hex= hex.replace(/^#/,'0x');
    var tem= [(hex >>16) & 255,(hex >> 8) & 255, hex & 255];
    return 'rgb('+tem.join(',')+')';
    }
    Last edited by mrhoo; 08-29-2008 at 08:36 AM.

  7. #7
    Join Date
    Jun 2003
    Location
    here
    Posts
    4,551
    Quote Originally Posted by Dok
    & is bitwise AND. This means that 1001 & 0001 = 0001 as only the first bit is set in both numbers.

    | is bitwise OR. This means that 1001 | 0001 = 1001 as the first and fourth bits are set in either number.
    ah, makes much more sense than what I assumed, also explains why it all fell apart at higher values as well. I shall make a mental note of that.
    If you are using PHP please use the [PHP] and [/PHP] forum tags for highlighting...
    The same applies to HTML and the forums [HTML][/HTML] tags.

  8. #8
    Join Date
    Aug 2008
    Posts
    7
    Oh thanks for all that !

    For anyone interested, here's the code I found on the web that was a tad puzzling (the bits that I posted):

    PHP Code:
    <pre id=D></pre><script>A=0,B=0,D=document.getElementById('D'),b=[];
    function 
    u(){n=Math.sin(B);for(k=0;1840>k;k++)y=-10-k/80|0,o=41+(k%80
    -40)*1.3/y+n|0,N=A-100.0/y,b[k]=k%80==79?"\n":".#"[o+N&1];D.innerHTML
    =b.join('');A+=0.05;B+=0.03;setTimeout(u,10);}u();</script> 
    copy, paste, save, open with a browser. Fun =-)

  9. #9
    Join Date
    Jan 2005
    Location
    Los Angeles, CA
    Posts
    4,887
    Wow nice 3D ascii text animation.
    The mathematical algorithm intrigues me and I'll study it some more in the hope of an insight into equation-based graphics which I haven't touched since the TI-86 days.
    Last edited by Ultimater; 08-29-2008 at 05:10 PM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles