Computer Science

Javascript Bitwise Operators

Javascript Bitwise Operators are used to manipulate the binary representation of numbers. These operators perform operations on individual bits of a number, allowing for efficient and precise calculations. The bitwise operators include AND, OR, XOR, NOT, left shift, and right shift.

Written by Perlego with AI-assistance

6 Key excerpts on "Javascript Bitwise Operators"

  • Book cover image for: Coding All-in-One For Dummies
    • Chris Minnick(Author)
    • 2022(Publication Date)
    • For Dummies
      (Publisher)
    Bitwise operators are difficult to understand at first. They’re not very commonly used in JavaScript, but they’re included here for the sake of completeness. Table 5-4 lists the Javascript Bitwise Operators. Working with Operators, Expressions, and Statements CHAPTER 5 Working with Operators, Expressions, and Statements 235 Figure 5-2 shows a demonstration of each of the bitwise operators in the Chrome JavaScript Console. TABLE 5-4 Javascript Bitwise Operators Operator Usage Description Bitwise AND a & b Returns a 1 in each bit position for which the corresponding bits of both operands are 1s. Bitwise OR a | b Returns a 1 in each bit position for which the corresponding bits of either or both operands are 1s. Bitwise XOR a ^ b Returns a 1 in each bit position for which the corresponding bits of either but not both operands are 1s. Bitwise NOT ~a Inverts the bits of its operand. Left shift a << b Shifts a in binary representation b (<32) bits to the left, shifting in zeros from the right. Sign-propagating right shift a >> b Shifts a in binary representation b (<32) bits to the right, discarding bits shifted off. Zero-fill right shift a >>> b Shifts a in binary representation b (<32) bits to the right, discarding bits shifted off and shifting in zeros from the left. FIGURE 5-2: The Javascript Bitwise Operators. © John Wiley & Sons 236 BOOK 3 Advanced Web Coding Logical operators The terms truthy and falsy in JavaScript refer to the value that’s returned when you evaluate something as a Boolean. The following values evaluate to Boolean false values and are thus considered falsy values: » false » 0 » -0 » '' (empty string) » null » undefined » NaN Everything else evaluates to a Boolean true value, and is thus considered truthy.
  • Book cover image for: Programming Interviews Exposed
    eBook - PDF

    Programming Interviews Exposed

    Coding Your Way Through the Interview

    • John Mongan, Noah Suojanen Kindler, Eric Giguère(Authors)
    • 2018(Publication Date)
    • Wrox
      (Publisher)
    The bitwise opera- tors in C#, Java, and JavaScript are the same as C and C++ except for the shift operators. The simplest bit operator is the unary operator ( ~) called NOT . This operator flips or reverses all the bits that it operates on. Thus, every 1 becomes a 0, and every 0 becomes a 1. For example, if ~ is applied to 00001101, then the result is 11110010. Three other bitwise operators are | (OR), & (AND), and ^ (XOR). They are all binary operators applied in a bitwise fashion. This means that the i th bit of one number is combined with the i th bit of the other number to produce the i th bit of the resulting value. The rules for these operators are as follows: ➤ & If both bits are 1, the result is a 1. Otherwise, the result is 0. For example: 01100110 & 11110100 01100100 ➤ | If either bit is a 1, the result is 1. If both bits are 0, the result is 0. For example: 01100110 | 11110100 11110110 ➤ ^ If the bits are the same, the result is 0. If the bits are different, the result is 1. For example: 01100110 ^ 11110100 10010010 Don’t confuse the bitwise & and | operators with the logical && and || operators. The bitwise opera- tors take two integers and return an integer result; the logical operators take two booleans and return a boolean result. 226 ❘ CHAPTER 14 GRAPHICS AND BIT MANIPULATION The remaining bit operators are the shift operators: operators that shift the bits within a value to the left or the right. C, C++, and C# have left (<<) and right (>>) shift operators. Java and JavaScript have one left-shift (<<) operator but two right-shift (>> and >>>) operators. The value to the right of the operator indicates how many positions to shift the bits. For example, 8 << 2 means shift the bits of the value “8” two positions to the left. Bits that “fall off” either end of a value (the overflow bits) are lost. The << operator is common to all five languages. It shifts the bits to the left, filling the empty bits on the right with 0.
  • Book cover image for: JavaScript for Web Warriors
    • Patrick Carey, Sasha Vodnik, Patrick Carey(Authors)
    • 2021(Publication Date)
    CHAPTER 2 WORKING WITH FUNCTIONS, DATA TYPES, AND OPERATORS 52 JavaScript operators are binary or unary. A binary operator requires an operand before and after the operator. The equal sign in the statement myNumber = 100; is an example of a binary operator. A unary operator requires just a single operand either before or after the operator. For example, the increment operator (++), an arithmetic operator, is used to increase an operand by a value of one. The statement myNumber++; changes the value of the myNumber variable to 101. Another type of JavaScript operator, bitwise operators, operate on integer values; this is a fairly complex topic. Bitwise operators and other complex operators are beyond the scope of this book. Note The operand to the left of an operator is known as the left operand, and the operand to the right of an operator is known as the right operand. Note Arithmetic Operators Arithmetic operators are used in JavaScript to perform mathematical calculations, such as addition, subtraction, mul- tiplication, and division. You can also use an arithmetic operator to return the modulus of a calculation, which is the remainder left when you divide one number by another number. Figure 2-8 describes the arithmetic operators supported by JavaScript. You might be confused by the difference between the division ( / ) operator and the modulus (%) operator. The division operator performs a standard mathematical division operation so that the expression 15/6 returns a value of 2.5. The modulus operator returns the remainder after a division so that the expression 15%6 returns a value of 3 because that is the remainder when 15 is divided by 6. Arithmetic operations can also be performed on a single variable using unary operators. Figure 2-9 lists the arithmetic unary operators available in JavaScript.
  • Book cover image for: Clean Code in JavaScript
    eBook - ePub

    Clean Code in JavaScript

    Develop reliable, maintainable, and robust JavaScript

    dynamic typing , we explored topics such as type-coercion and detection; we also covered several operators. In this chapter, we'll continue this exploration by delving into every single operator that the JavaScript language makes available. Having a rich understanding of JavaScript's operators will make us feel utterly empowered in a language that can, at times, appear confusing. There is, unfortunately, no shortcut to understanding JavaScript, but as you begin to explore its operators, you will see patterns emerge. For example, many of the multiplicative operators work in a similar manner, as do the logical operators. Once you are comfortable with the main operators, you will begin to see that there is a grace underlying the complexity.
    It may be useful to treat this chapter as more of a reference if you're pressed for time. Do not feel like you need to exhaustively retain every detail of every operator's behavior. In this chapter, we will cover the following topics:
    • What is an operator?
    • Arithmetic and numeric operators
    • Logical operators
    • Comparative operators
    • Assignment operators
    • Property access operators
    • Other operators and syntax
    • Bitwise operators
    Now that we're ready to dive in, the very first question we need to ask ourselves is: what even is an operator?
    Passage contains an image

    What is an operator?

    An operator in JavaScript is a standalone piece of syntax that forms an expression and is typically used to derive something or compute a logical or mathematical output from a set of inputs (called operands ).
    Here, we can see an expression containing an operator (+) with two operands (3 and 5): 3 + 5 Any operator can be said to have four characteristics:
    • Its arity : how many operands the operator accepts
    • Its function : what the operator does with its operands and what it evaluates to
    • Its precedence : how the operator will be grouped when used in combination with other operators
    • Its associativity : how the operator will behave when neighbored with operators of the same precedence
    It's important to understand these foundational characteristics as it will vastly aid your usage of operators in JavaScript.
    Passage contains an image

    Operator arity

    Arity refers to how many operands (or inputs
    ) an operator can receive. An operand is a formal term for the value(s) that you can
    give or pass to an operator.
    If we consider the greater-than operator (>), it receives two operands: a > b
    In this example, a is its first operand (or left-side operand). And b
  • Book cover image for: Microchip AVR Programming using ATmega Microcontrollers
    • Ivan Volosyak(Author)
    • 2021(Publication Date)
    • Shaker
      (Publisher)
    82 5 Basic Microcontroller Programming Since the postfix operator is used in the variable number1, its original value is used (16) and converted into the string. Then it is increased by 1 and becomes 17 and converted into the text string. As the order of such operations is not clear (number1++ is in the same code line as number1) and may also differ between compilers, the following warning will be shown: “main.c:10: warning: operation on ’number1’ may be undefined”. Finally, the value of number2 is increased and used in the sprintf() function, so the result is 17. Bitwise operators perform operations on every bit of the data. These operators are particu- larly important in microcontroller programming in order to set bits in specified registers. The bitwise operators are bitwise AND (&), bitwise OR (|), bitwise XOR (^), bitwise NOT (~), shift left (<<) and shift right (>>). Table 5.4 shows the truth tables for bitwise AND (&), OR (|), XOR (^), and NOT (~) operators. Table 5.4: Truth Tables for Bitwise Operators: AND, OR, XOR, NOT. Bitwise AND Bitwise OR Bitwise XOR Bitwise NOT a b a & b a b a | b a b a ^ b a b ~a ~b 0 0 0 0 0 0 0 0 0 0 0 1 1 0 1 0 0 1 1 0 1 1 0 1 1 0 1 0 0 1 0 1 1 0 1 1 0 0 1 1 1 1 1 1 1 1 1 0 1 1 0 0 With the help of these “truth tables”, it is possible to easily understand the bitwise operations performed on 8-bit values, as shown in the examples in Table 5.5. Table 5.5: Bitwise Operators: Mode of Operation for NOT, AND, OR, and XOR operators.
  • Book cover image for: Introduction to Java Programming, 2nd Edition
    These operators are the least commonly used operators. Some of the bitwise operators are categorized under bitwise logical operators and these are discussed next.  
    The Bitwise Compliment (~) Operator
    The bitwise Compliment (~ ) operator comes under the category of bitwise logical operators. The ~ operator inverts all bits of its operand; for example, 0 becomes 1 and 1 becomes 0. This operator is also known as the bitwise unary NOT operator. The syntax for using the compliment (~ ) operator is as follows:
      ~ value or expression;   For example:   int a = 3; int b = ~a;  
    In this example, 3 is assigned to the integer variable a as an initial value, which is stored in the computer’s memory as 00000011. In the next statement, ~ operator is used with the integer variable a . This operator inverts all the bits 00000011 of the value 3 into 11111100. Then, the resultant value is assigned to the integer variable b .
     
    The Bitwise AND (&) Operator
    The bitwise AND (& ) operator also comes under the category of bitwise logical operators. If both the operands consist of the value 1, then the & operator will produce bit 1 as the result. But, if one or both the operands consist of the value 0, then the & operator will produce 0 as the result.
     
    The syntax for using the & operator is as follows:
      operand1 & operand2;  
    For example, you can use the AND (& ) operator with two operands: 23 and 15, as given next:
      00010111//Bits representing the value 23 & 00001111//Bits representing the value 15 -------- 00000111//Bits representing the value 7  
    The Bitwise OR (|) Operator
    The bitwise OR (| ) operator also comes under the category of bitwise logical operators. If one or both the operands consist of the value 1, then the | operator will produce bit 1 as the result. But, if both the operands contain 0, then the | operator will produce 0 as the result. The syntax for using the |
Index pages curate the most relevant extracts from our library of academic textbooks. They’ve been created using an in-house natural language model (NLM), each adding context and meaning to key research topics.