Online Documentation Server
 ПОИСК
ods.com.ua Web
 КАТЕГОРИИ
Home
Programming
Net technology
Unixes
Security
RFC, HOWTO
Web technology
Data bases
Other docs

 


 ПОДПИСКА

 О КОПИРАЙТАХ
Вся предоставленная на этом сервере информация собрана нами из разных источников. Если Вам кажется, что публикация каких-то документов нарушает чьи-либо авторские права, сообщите нам об этом.




Previous Table of Contents Next

At first, you might think that these statements are equivalent. They aren’t, because the first one uses the string operator, and the second one uses commas to delimit strings and numbers. They differ more than in style. In the first statement, the expression between the parentheses is evaluated to a single string—"I have 2 cookies." Therefore, the document.write() method in this statement prints only one expression. The second statement prints multiple expressions. The literals are not evaluated to a single value as in the first statement but are rather printed independently. Both statements print the exact same HTML to the page, but they do it in different ways. In order to understand how each statement works, take a look at the following sequences of statements. The first sequence is equivalent to the first statement in the previous set, and the second sequence is equivalent to the second statement.

// sequence #1
var stringToPrint =  "I have " + 2 + " cookies."
document.write(stringToPrint)

// sequence #2
document.write("I have ")
document.write(2)
document.write(" cookies.")

A common mistake made by beginners and advanced programmers alike is to forget spaces in strings. A space is a character just like any other one. Forgetting a space character is not a severe mistake, because you can easily locate where to add the space. You should use one of the following statements to print two consecutive numbers with a separating space character in between:

document.write(16 + " " + 18) // first possibility
document.write(16, " ", 18) // second possibility

The first statement is valid because of the automatic casting method used by JavaScript’s interpreter.


Tip:  The plus (+) operator in JavaScript does not convert string operands to numeric values. The reason behind this is that the string concatenation operator and the plus operator use the same character: +. If only one operand is a string, the other is converted to a string value and then the operator concatenates the strings. In Perl, the plus (addition) and minus (subtraction) operators convert their arguments from strings to numeric values if necessary, and return a numeric result. This feature is made possible because the string concatenation operator in Perl is . (dot), not +, which is shared with another operator (addition) in JavaScript. C++ and Pascal feature a function that concatenates strings.


Bitwise Operators

Bitwise operators are the operators used in bit-oriented operations. A bit is the smallest unit of information, usually represented by 0 or 1 specifications. Bit manipulations are used to control the machine at the lowest level. If you plan to program at a higher level, this section may be safely skipped. In JavaScript you won’t be using bitwise operators to control the machine at a low level but rather for other purposes such as encrypting and encoding.

Eight consecutive bits form a byte. There are 256 (28) byte variations. That is, a byte can be one of 256 eight-bit sequences. For example, 11010001 is one of these 256 possibilities. A byte is represented by a character in programming languages that support character data types, such as C, C++, and Pascal.

Hexadecimal notation is convenient for representing binary data because each hexadecimal digit represents four binary bits. Table 7-2 lists the hexadecimal values from 0 to F along with the equivalent binary values.

Table 7-2. Hexadecimal and binary equivalence.


Hexadecimal Binary Hexadecimal Binary

0 0000 8 1000
1 0001 9 1001
2 0010 A 1010
3 0011 B 1011
4 0100 C 1100
5 0101 D 1101
6 0110 E 1110
7 0111 F 1111

Based on the obvious pattern, you should be able to reproduce it blindly.

Bitwise operators enable the scripter to work on individual bits. The bitwise (bit) operators in JavaScript are listed in Table 7-3.

Table 7-3. Bitwise operators in JavaScript.


Syntax Name Type

& Bitwise AND binary
| Bitwise OR binary
^ Bitwise XOR (exclusive OR) binary
~ Bitwise NOT unary
<< Left shift binary
>> Right shift binary
>>> Zero-fill right shift binary

Bitwise AND

operand1 & operand2

The bitwise AND operator compares two bits. The only situation in which the result is 1 is when both bits are 1. Here is the truth table for this operator:

Table 7-4. Bitwise AND truth table.


Bit1 Bit2 Bit1 & Bit2

0 0 0
0 1 0
1 0 0
1 1 1

The AND operator, like all other bitwise operators, can take only a numeric value as its operand.

Although you will probably never use bitwise operators, let’s learn how the results are calculated. All calculations will be performed in hexadecimal and binary bases, because they are most convenient for the task. Remember that hexadecimal numbers have a “0x” prefix.

Let’s take a look at the following numbers, and how the bitwise AND operates on them:

  0x23 001000112
& 0x72 011100102
= 0x22 001000102

0x23 & 0x72 // evaluates to 34 (= 0x22 = 2216)

The bitwise AND operator is similar to the logical AND operator, which is discussed later in this chapter.

You can use the bitwise AND operator to test whether a number is even or odd. In binary (base 2), the last digit of an odd number is 1, and the last digit of an even number is 0. The following function uses the bitwise AND operator to determine whether the number is odd or even. It returns true if decimalNumber is even, and false if it is odd.

function checkEven(decimalNumber) {
  return (decimalNumber & 1 == 0)
}

Previous Table of Contents Next


With any suggestions or questions please feel free to contact us