JavaScript
  Home arrow JavaScript arrow Page 2 - The Power of Javascript: Operators continu...
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVASCRIPT

The Power of Javascript: Operators continued
By: Michael Youssef
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 8
    2005-07-26

    Table of Contents:
  • The Power of Javascript: Operators continued
  • Bitwise Operators
  • The Bitwise Operators Example
  • Assignment Operators
  • Assignment Operators Example

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    The Power of Javascript: Operators continued - Bitwise Operators


    (Page 2 of 5 )

    Bitwise operators are used to perform boolean logical operations (which will be explained shortly) on their operands after converting their numerical representation to binary representation (bits). Javascript stores numerical data in 64-bit variables, but when you use bitwise operators, the interpreter converts the operands to 32-bit integer values. Any fractional value will be removed.

    If the value is bigger than the range of the 32-bit value, the interpreter fits the value into 32-bits. If one of the operands is not a numerical value (something like "hi"), the expression evaluates to -1. If the value can be converted to a numerical representation (something like "23"), the interpreter automatically converts it. There are seven bitwise operators available. Let's take a look.

    Operator

    Operator Explanation

    &

    The bitwise AND operator performs a boolean AND operation on the bits of its integer operands. As you know, 1 means that the circuit is turned on, which also means true, and 0 means false. A logical boolean operation produces the value true or false based on the value of each bit in both operands. In the case of the bitwise AND operator it produces 1 if both postition matched bits are 1, otherwise it produces 0 -- because it's like saying return true if the bit in the first operand is true AND the bit in the second operand is true. The statement result = 8 & 3; produces the value 0. If we presented it as the binary representation it will be clearer:

     00001000 (8 in binary)
    &         (using Bitwise AND)
     00000011 (3 in binary)
    ---------
     00000000 (0 in decimal)

    8 is represented in binary as 00001000 (note that I have removed the rest of the 32 bits because the value is small enough to be represented in 1 byte, 8 bits) and 3 is represented as 11 in binary. There are no matched bits that have the value 1, so the product is 00000000.

    |

    The Bitwise OR Operator performs an operation that sets the resulting bit to 1 if the first operand's bit is 1 OR if the second operand's bit is 1 OR even if both of them are 1. It's much more like saying set the resulting bit to one if any of the position matched bits are 1, so the statement result = 8 | 3; evaluates to 11. Let's take a look at the binary representation:

     00001000
    |
     00000011
    ---------
     00001011

    The binary data 00001011 means in decimal 8 + 2 + 1 which equals to 11.

    ^

    The bitwise XOR operator (or the exclusive OR operator) is the same as the bitwise OR operator with one difference: it doesn't set the resultant bit to 1 if both bits are 1. It sets the resultant bit to 1 if only one of the operands' position matched bits is 1. If both of the bits have the value 1, the ^ operator puts 0 in the resultant bit. So the statement result = 7 ^ 3; produces the value 4

     00000111
    ^
     00000011
    ---------
     00000100

    the binary value 00000100 equals 4.

    ~

    The bitwise NOT operator is the only unary bitwise operator. It simply changes the state of all the bits in the value; it turns all 1s to 0s and all 0s to 1s. So the statement
    result = ~4294967295; produces the value 0 because the decimal value 4294967295 is represented in binary as 32 ones. As you know, when using bitwise operators, the interpreter converts the value to 32-bits, and using the bitwise NOT operator turns all the 1s to 0s, which is why we got the value 0

    ~ 11111111111111111111111111111111
    ----------------------------------
      00000000000000000000000000000000

    <<

    The left shift operator doesn't set bits. Instead it moves the bits (of the value in the left operand) to the left by the value of the right operand. Take the value 3 for example, when we shift its binary value by 1 as in the statement result = 3 << 1; it produces the value 6. Remember, 3 is represented in the binary system as 00000011, so when we shift it one time to the left, the value becomes 00000110. Note that when we shift the value to the left it puts 0s on the right.

    >>

    The right shift operator shifts bits from left to right. The statement result = 255 >> 5; produces the value 7 because 255 is represented in binary as 11111111. When we shift 5 bits, the value becomes 00000111. Shifting from right is like cutting the number of bits and putting 0s on the left.

    >>>

    The right shift with zero fill operator is the same as the >> operator, but it doesn't reserve the sign of the value. We have not discussed how we can reserve the sign in the binary representation so we will not talk about this operator now. I will introduce this concept to you later in the series.

    More JavaScript Articles
    More By Michael Youssef


     

    JAVASCRIPT ARTICLES

    - Building Dynamic Shadows with JavaScript and...
    - Active Client Pages: Chrys`s Approach
    - The Script Approach to Active Client Pages: ...
    - Principles of Active Client Pages: the Scrip...
    - Active Client Pages: the Script Approach
    - Building an RTF-capable Form with the Ext JS...
    - Creating a Multi-Tabbed Online Form with the...
    - jQuery Overview
    - Constructing a Multi-Column Online Form with...
    - Grouping Field Sets on Dynamic Web Forms wit...
    - Building Dynamic Web Forms with the Ext JS F...
    - More on JavaScript Array Objects
    - Methods of the DOM Location Object
    - The DOM Location Object Properties
    - Handling Remote Files with JavaScript Click ...







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
    Stay green...Green IT