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.


undefined
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.


undefined
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.

undefined
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