Computer Science
Java Bitwise Operators
Java Bitwise Operators are used to perform operations on individual bits of binary numbers. These operators include AND, OR, XOR, NOT, left shift, and right shift. They are commonly used in programming to manipulate data at the bit level.
Written by Perlego with AI-assistance
Related key terms
1 of 5
6 Key excerpts on "Java Bitwise Operators"
- 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. - eBook - PDF
- Ivor Horton(Author)
- 2005(Publication Date)
- Wrox(Publisher)
So if antique computers are your thing, this may turn out to be a valuable technique. In fact, it’s really much more useful than that. When you get to do some graphics programming later in the book, you’ll see that this application of the exclusive OR operator is very relevant. Don’t forget — all of these bitwise operators can be applied only to integers. They don’t work with any other type of value. As with the arithmetic expressions, the bitwise operations are carried out with 32 bits for integers of type short and of type byte, so a cast to the appropriate type is necessary for the result of the expression on the right of the assignment operator. 69 Programs, Data, Variables, and Calculation One note of caution: Special care is needed when initializing variables of type byte and type short with hexadecimal values to avoid being caught out. For example, you might be tempted to initialize a vari- able of type byte to binary 1111 1111 with the following statement: byte allBitsOne = 0xFF; // Wrong!! In fact, this results in a compiler error message. The literal 0xFF is 1111 1111, so what’s the beef here? The beef is that 0xFF is not 1111 1111 at all. The literal 0xFF is type int, so it is the binary value 0000 0000 0000 0000 1111 1111. This happens to be equivalent to the decimal value 128, which is outside the range of type byte. The byte value you are looking for, 1111 1111, is equivalent to the decimal value -1, so the correct way to initialize allBitsOne to 1s is to write: byte allBitsOne = 0xFFFFFFFF; // Correct – well done!! Now the compiler will happily chop off the high-order bits to produce the result you are looking for. Shift Operations Another mechanism that you have for working with integer variables at the bit level is shifting. You can shift the bits in an integer to the right or the left. You can also envisage the process of shifting binary dig- its right or left as dividing or multiplying by powers of two, respectively. - eBook - ePub
- Prof. Sham Tickoo(Author)
- 2017(Publication Date)
- CADCIM Technologies(Publisher)
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 (~) OperatorThe 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 (&) OperatorThe 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 7The Bitwise OR (|) OperatorThe 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 | - eBook - ePub
Embedded Systems
A Contemporary Design Tool
- James K. Peckol(Author)
- 2019(Publication Date)
- Wiley(Publisher)
unsigned int. We don't want one of the bits to be interpreted as a sign.Common operations that one might perform include:- Setting or resetting bits on a microprocessor or microcontroller output port.
- Testing status bits on input lines or in registers.
- Setting or resetting status bits as the result of some operation.
- Making comparison operations.
- Quickly performing certain multiplication or division operations.
The application of each operator is rather straightforward and follows naturally from the logical operators. Let's examine some examples of these to see how they might apply in our designs. We will first look at some simple bit manipulation operations.7.2.1 Bit Manipulation Operations
each of the individual bitsThe logical bitwise operators are binary operators that return the result of the logical AND, OR, or XOR of each of the individual bits in the two operands. The code module in Figure 7.1 illustrates how each of these works.Figure 7.1 Working with Bitwise Operators7.2.2 Testing, Resetting, and Setting Bits
We can use the logical bitwise operators to determine whether a specific bit within a word is set or reset – that is, has a value of logical 1 or logical 0.We have executed the following code to read the state of an I/O port on a microprocessor, and we wish to test whether bit 5, a status bit, is set. If it is, we wish to reset it, acknowledging the event flagged by the status bit, and we set bit 3 to initiate some action in the external device.setPort()We will assume that the port comprises eight bits, one byte. We further assume that we have the I/O support function, setPort() - eBook - ePub
Java
The Comprehensive Guide
- Christian Ullenboom(Author)
- 2022(Publication Date)
- SAP PRESS(Publisher)
(a && !b) || (!a && b) .boolean aboolean b! aa && ba || ba ^ btruetruefalsetruetruefalsetruefalsefalsefalsetruetruefalsetruetruefalsetruetruefalsefalsetruefalsefalsefalseTable 2.11 Links of the Logical Operators NOT, AND, OR, and XORThe logical operators always work on the boolean type. In Chapter 22 , Section 22.1.1 , we’ll see that the same operations can be performed on any bit of an integer.[»] Outlook on Propositional Logic*Links of this kind are rather important in propositional logic or Boolean algebra. The terms AND, OR, and XOR, which are common for us, might also be known under different names. The AND link is called a conjunction , the OR link is called a disjunction , and the exclusive OR is called a contravalence . The three binary operators AND, OR, and XOR cover certain linkages, not all of which are possible. Propositional logic also features implication (if-then linkage) and equivalence .2.4.8 Short-Circuit Operators
A logical expression only needs to be evaluated further if the final result can still change. If the result is already irrefutably fixed before the evaluation of all parts, the compiler shortens the program flow. The two operators && (AND) and || (OR) are useful for optimizing expressions:-
AND : If one of the two expressions is false, the expression can’t be true. The result is false.
-
OR : If at least one of the expressions is true, then the entire expression is also true.
Take, for example, true || Math.random() > 0.5 . In this case, the method won’t be called because the two operators && and || are short-circuit operators - eBook - PDF
- Kunal Pimparkhede(Author)
- 2017(Publication Date)
- Cambridge University Press(Publisher)
3.5.2 Bitwise OR Operation (¦) The operator ¦ is used to perform a bitwise OR operation. Hence, the statement, b=p¦q; will perform the bit-by-bit logical OR operation over the operands p and q , thereby storing the result of the OR operation in the variable b . We know that the result of the OR operation is 1 when any one of the input bits is 1. Given below is the truth table of the logical OR operation: Input bit 1 Input bit 2 Result of OR operation 0 0 0 0 1 1 1 0 1 1 1 1 Given below is the evaluation of the statement b=p|q ; using the truth table of the logical ‘OR’ operation: p = 0000 0000 0001 0100 q = 0000 0000 0000 1101 b = 0000 0000 0001 1101 Hence, the value of the variable b will be set as 29 in the decimal number system, because the binary number 0000 0000 0001 1101 is equivalent to a decimal value of 29. 3.5.3 Bitwise Ex-OR Operation (^) The operator ^ is used to perform a bitwise Ex-OR operation. Hence, the statement, c = p^q; will perform a bit-by-bit logical Ex-OR operation over the operands p and q , thereby storing the result of Ex-OR operation in the variable c . We know that the result of Ex-OR Operators and Type Casting ✦ 87 operation is 1 when the ODD number of input bit is 1. Given below is the truth table for the logical Ex-OR operation: Input bit 1 Input bit 2 Result of Ex-OR operation 0 0 0 0 1 1 1 0 1 1 1 0 Given below is the evaluation of the statement c = p^q; using the truth table of the bitwise Ex-OR operation: p = 0000 0000 0001 0100 q = 0000 0000 0000 1101 c = 0000 0000 0001 1001 Hence, the value of the variable c will be set as 25 in decimal number system, because a binary number 0000 0000 0001 1001 is equivalent to a decimal number 25. 3.5.4 One’s complement operator (~) One’s complement of any binary number is obtained by bit inversion. Bit inversion is a process to replace every occurrence of 1 by 0 and every occurrence of 0 by 1 in a binary number.
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.





