Szczęście znów zamieszkało w sercu

W duszy znów zagościło szczęście

Już któryś raz zauważyła Kinga, jak jej mąż Marek przytrzymuje się za lewy bok, gdzie serce. Starał się niepostrzeżenie, lekko pogładzi i odsunie dłoń, rozglądając się, czy żona nie widzi. A ona nie raz pytała:

Znowu boli, Marek? Trzeba by do lekarza w powiecie się wybrać.

Przejdzie, zdarza się, zaraz minie odpowiadał zawsze tak samo.

Dziewiąty rok żyli razem Kinga i Marek we wsi, do której przyjechali po skończeniu studiów. Marek uczył się w szkole rolniczej, ona na pedagogice. Ale Kinga nigdy nie pracowała, bo mąż kochał gospodarstwo, a całe podwórko pełne było zwierząt. Dwie krowy, owce, prosiak, kury i kaczki. Wszystkim trzeba było się zająć. Więc żona była w domu, na nogach cały dzień. Marek pracował jako agronom.

Kingę od trzynastego roku życia wychowywała babcia, bo rodzice zginęli młodo spłonęli w domu, a ona akurat tej nocy była u babci. Marek pochodził z tej wsi. Ale po ślubie, po trzech latach, zmarł jego ojciec atak# Bitwise operators

Bitwise operators perform operations on **bit patterns** (binary numerals) at the level of their individual bits. They are used in numerical computations to make the calculation process faster.

All data in computer programs is internally stored as bits, i.e., as numbers consisting of 0’s and 1’s. A bit is the smallest unit of information that can be stored by a computer. Each bit has a single binary value: 0 or 1.

In Kotlin, you can apply bitwise operations to integers (**Int** and **Long**). To perform them, you need to convert numbers from the decimal to the binary system. The result will also be in the binary system, so you need to convert it back to the decimal system to understand it.

## Bitwise operators

There are seven bitwise operations:

– `and` (bitwise conjunction)
– `or` (bitwise disjunction)
– `xor` (bitwise exclusive OR)
– `inv` (bitwise inversion)
– `shiftLeft` (logical left shift)
– `shiftRight` (logical right shift)
– `ushr` (unsigned right shift)

All operations except inv are **binary**, which means they take two arguments. Inv is a **unary** operation, it takes a single argument.

Bitwise operations can be invoked as infix functions only. `and`, `or`, and `xor` can be invoked with the usual way, but `inv` is different because it is a unary operator.

“`kotlin
val a = 5
val b = 6

println(a and b) // 4
println(a or b) // 7
println(a xor b) // 3
println(a.inv()) // -6
println(a.shl(2)) // 20
println(a.shr(2)) // 1
println(a.ushr(2)) // 1
“`

## How bitwise operations work

Let’s take two numbers, a = 5 and b = 6, and perform all bitwise operations on them.

### Step 1: Converting to binary

First, we need to convert these numbers to their binary representation.

– The number 5 in binary is 0000 0101 (actually, it’s 00000000000000000000000000000101 in 32-bit integer representation, but we’ll omit leading zeros for simplicity).
– The number 6 in binary is 0000 0110.

### Step 2: Performing operations

Now, let’s perform each operation step by step.

### Bitwise AND (and)

The `and` operation compares corresponding bits of two numbers and returns 1 if both bits are 1, otherwise 0.

“`
5: 0000 0101
& 6: 0000 0110
———–
0000 0100 (which is 4 in decimal)
“`

### Bitwise OR (or)

The `or` operation compares corresponding bits and returns 1 if at least one of the bits is 1.

“`
5: 0000 0101
| 6: 0000 0110
———–
0000 0111 (which is 7 in decimal)
“`

### Bitwise XOR (xor)

The `xor` operation compares corresponding bits and returns 1 if the bits are different, and 0 if they are the same.

“`
5: 0000 0101
^ 6: 0000 0110
———–
0000 0011 (which is 3 in decimal)
“`

### Bitwise Inversion (inv)

The `inv` operation inverts all bits of the number, changing 0 to 1 and 1 to 0. The result depends on the number of bits used to represent the operand. For simplicity, let’s consider 8-bit representation. However, in Kotlin, integers are 32-bit, so the inversion of 5 (000…000101) becomes 111…111010, which is -6 in decimal due to two’s complement representation.

“`
5: 0000 0101
~ (invert)
———–
1111 1010 (which is -6 in decimal in 8-bit two’s complement)
“`

### Left Shift (shl)

The `shl` operation shifts the bits of the number to the left by a specified number of positions, filling the vacated bits with zeros. Here, we shift 5 (0000 0101) left by 2 positions.

“`
5: 0000 0101
shl 2
———–
0001 0100 (which is 20 in decimal)
“`

### Right Shift (shr)

The `shr` operation shifts the bits of the number to the right by a specified number of positions, filling the vacated bits with the sign bit (the leftmost bit). Here, we shift 5 (0000 0101) right by 2 positions.

“`
5: 0000 0101
shr 2
———–
0000 0001 (which is 1 in decimal)
“`

### Unsigned Right Shift (ushr)

The `ushr` operation shifts the bits of the number to the right by a specified number of positions, filling the vacated bits with zeros, regardless of the sign bit. For positive numbers, shr and ushr give the same result. For 5 (0000 0101):

“`
5: 0000 0101
ushr 2
———–
0000 0001 (which is 1 in decimal)
“`

### Summary

– `and`: 5 and 6 4
– `or`: 5 or 6 7
– `xor`: 5 xor 6 3
– `inv`: 5.inv() -6
– `shl`: 5 shl 2 20
– `shr`: 5 shr 2 1
– `ushr`: 5 ushr 2 1 (same as shr for positive numbers)

## Conclusion

Bitwise operations are powerful tools for manipulating individual bits in numbers. They are essential in low-level programming, such as embedded systems or performance-critical applications. Understanding how these operations work can help you write more efficient and optimized code.

Rate article
Fajna Tajna
Szczęście znów zamieszkało w sercu