CodeGym /Courses /Java Syntax Zero /Bitwise operations in Java

Bitwise operations in Java

Java Syntax Zero
Level 9 , Lesson 5
Available

1. Bitwise & operator

We previously said that all data is stored in memory in a binary representation. So quite a long time ago, programmers came up with a lot of interesting ways to work with binary numbers. For example, Java has logical operators that operate on the bits of a number's binary representation: & (AND), | (OR), ~ (NOT or complement) and ^ (XOR - exclusive or).

a & b
Bitwise & (AND) operator

This operator is very similar to the logical & (AND) operator, only it is denoted by a single ampersand, not two:

And it is applied to individual bits. Each operand is treated as an array of bits, and the ith bit of the result is calculated using the ith bit of each of the two operands.

The first bit of the result will be calculated based on the first bit of the number a and the first bit of the number b, the second bit — based on the second bit of the number a and the second bit of the number b, etc.

The & (AND) operator means "the resulting bit is equal to one only if the corresponding bit of the number a is equal to one AND the corresponding bit of the number b is equal to one":

1 & 1 = 1
1 & 0 = 0
0 & 1 = 0
0 & 0 = 0

Examples:

Example Result
0b0011 & 0b1010
0b0010
0b1111 & 0b0000
0b0000
0b1010 & 0b0101
0b0000
0b1111 & 0b1010
0b1010

2. Bitwise | operator

This operator is very similar to the logical | (OR) operator, only it is denoted by a single vertical line, not two:

a | b

And it is applied to individual bits. Each operand is treated as an array of bits, and the ith bit of the result is calculated using the ith bit of each of the two operands.

The bitwise | (OR) operator means "the resulting bit is equal to one if the corresponding bit of the number a is equal to one OR the corresponding bit of the number b is equal to one":

1 | 1 = 1
1 | 0 = 1
0 | 1 = 1
0 | 0 = 0

Examples:

Example Result
0b0011 | 0b1010
0b1011
0b1110 | 0b0000
0b1110
0b1010 | 0b0101
0b1111
0b1111 | 0b1010
0b1111

Only when the corresponding bits (the bits at the same position) of both numbers are zero is the result's corresponding bit equal to zero.


9
Task
New Java Syntax, level 9, lesson 5
Locked
URL validation
In this task, you need to perform URL validation. A simple URL scheme looks like this: ://. The checkProtocol(String) method checks the network protocol (http or https) of the URL passed in input parameter and returns the result of this check — the name of the network protocol as a string. And the

3. Bitwise ^ (XOR or "exclusive or") operator

The XOR operator, also pronounced exclusive or, is denoted by the ^ symbol. To enter it on the keyboard, press shift + 6 (on a English keyboard layout).

a ^ b

This operator is somewhat similar to the OR operator, including in that it has a similar name: XOR

The bitwise ^ (XOR) operator means "the resulting bit is equal to one if the corresponding bit of the number a is equal to one OR the corresponding bit of the number b is equal to one but not both at the same time":

1 ^ 1 = 0
1 ^ 0 = 1
0 ^ 1 = 1
0 ^ 0 = 0

Examples:

Example Result
0b0011 ^ 0b1010
0b1001
0b1110 ^ 0b0000
0b1110
0b1010 ^ 0b0101
0b1111
0b1111 ^ 0b1010
0b0101

Only when the corresponding bits (the bits at the same position) of both numbers are different is the result's corresponding bit equal to one. If the bits are the same, the resulting bit is equal to zero.


9
Task
New Java Syntax, level 9, lesson 5
Locked
Searching in a string
The getIndexOfFirstWord(String, String) method and the getIndexOfLastWord(String, String) method both accept a string and a word. The getIndexOfFirstWord(String, String) method needs to return the index of the first character of the first instance of the word (the second method parameter) in the st

4. Bitwise ~ (NOT, COMPLEMENT) operator

I think you can already guess what it does. This operator is very similar to the logical ! (NOT) operator, but it is denoted by a tilde, not an exclamation point:

~a

This is a unary operator, which means it applies to a single number, not two. It appears before this single operand.

The bitwise ~ operator means "the resulting bit is one if the corresponding bit of the number a is zero, and it is zero if the corresponding bit of the number a is one":

~1 = 0
~0 = 1

Examples:

Example Result
~0b0011
0b1100
~0b0000
0b1111
~0b0101
0b1010
~0b1111
0b0000

This operator simply changes the bit that are 1 to 0 and bits that are 0 to 1.

9
Task
New Java Syntax, level 9, lesson 5
Locked
Path update
Implement the changePath(String, String) method so that it replaces the jdk version in the path passed in the first method parameter with the version passed in the second parameter, and returns the new path. The JDK version starts with "jdk" and ends at "/". Example: path - "/usr/java/jdk1.8/bin" J
Comments (6)
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION
Evgeniia Shabaeva Level 42, Budapest, Hungary
9 April 2024
I'm also wondering what this part of the code given in the last exercise means: @Override public String toString() { return String.format("x=%d, y=%d", x, y); } I know we are not supposed to do anything about it, but it is still interesting to understand.
Evgeniia Shabaeva Level 42, Budapest, Hungary
9 April 2024
I'm kind of fine with the tasks (done them successfully) but I still don't feel the lesson is completely clear to me. First of all, as someone has mentioned before, a chart of binary coded numbers would be really handy in this lesson. Secondly, it would be great if the lesson provided some examples of using the XOR operator in combination with the assignment one (like this: x ^= y). It seems we are supposed to arrive at understanding this ourselves, without any explanation.
24 September 2023
This lesson needs a major overhaul. It makes no sense what so ever with the information given.
Zac Level 17, Austin, United States
1 June 2023
CodGym team, some feedback on this lesson! :) I think it would be very helpful to do the following: (1) provide a table of the bit notation of, say, numbers 0 through 10 (2) provide some examples of how multiple iterations of bitwise operators can shift numbers! I suspect this lesson ends up being really challenging for people primarily because it throws folks into the deep end very quickly, and it isn't clear that students should be thinking in terms of bitwise notation. For anyone having trouble with this lesson, know that the lesson is asking you to think in terms of BIT notation. So for example, the number 4, represented in bit notation, is 0100; likewise, 5 is 0101.
Ozoda Izzatillaeva Level 27, Uzbekistan
16 May 2023
I still don't get this lesson
manny9876 Level 36, Israel
24 October 2022
from Google: Operands definition: In computer programming, an operand is a term used to describe any object that is capable of being manipulated. For example, in "1 + 2" the "1" and "2" are the operands and the plus symbol is the operator.