|
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.
Bitwise OperatorsBitwise 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.
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.
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.
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) }
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
With any suggestions or questions please feel free to contact us |