In this tutorial, you'll learn about different bitwise operations in Swift. These are used for bit level computation in an expression.
A bit is used to denote a binary digit. A binary digit can have two possible values either 0 or 1. As a beginner level programmer, you don't have to work with operations at the bit level.
Working with primitive data types such as: integer, float, boolean, string etc is enough. You may need to work at bit level when you are dealing with low level programming.
Swift provides a rich set of operators, apart from basic operators, to manipulate bits. These operators are similar to the logical operators, except that they work on binary representations of data (bits).
Bitwise operators are operators that are used to change individual bits of an operand. Operand is a variable or constant in which the operation is done.
It is represented by tilde ~ sign and can be applied on a single operand. This inverts all the bits. i.e changes 1 to 0 and 0 to 1.
In the above program, the statement let initalNumber:UInt8 = 1 is of type Unsigned int of size 8 bits. So, 1 in decimal can be represented as 00000001 in binary.
The bitwise not operator changes all the bit of a variable or constant, the bit 0 is changed to 1 and 1 to 0. So invertedNumber contains bits 11111110. After converting it into decimal it is represented as 254. So, the statement print(invertedNumber) outputs 254 in the screen.
initialBits contains binary value 11111111 that corresponds to 255 in decimal. To represent the number in binary we have 0b as a prefix in the literal. Without 0b as a prefix, it will treat it as a normal integer and you will get an overflow error (UInt8 can store numbers from only 0 to 255).
Since, we have used bitwise not operator, it changes all the 1 to 0. So, the constant invertedBits contains 00000000 which is equivalent to 0 in UInt8.
In the above program, 1 in decimal can be represented as 00000001 in binary. The bitwise not operator changes all the bit of a variable or constant, the bit 0 is changed to 1 and 1 to 0. So, invertedNumber contains bits 11111110. This should output 254 in the screen. But instead returns -2. Strange, Right?? Let's explore below how did this happen.
let initalNumber:Int = 1 is a signed int that can hold both positive and negative integers. That's why when we applied not operator for a signed integer, the returned binary may also represent a negative number.
How did compiler interpreted -2 as 11111110 in binary?
The compiler used Two's complement to represent integers. To get the two's complement negative notation of an integer, you should first write out the number in binary then invert the digits, and add one to the result.
That's how compiler interprets binary number 1111110 as -2 in decimal. But, there is a little twist that compiler made which we did not notice. It also inferred the type of invertedNumber as Int8 type.
In the above example, compiler treated the binary number to -2 in decimal only for the Signed 8-Bit Integer. Therefore statement print(Int8(bitPattern: 0b11111110)) outputs -2 in the screen.
But for the normal integer type whose size is 32/64 bit and can hold large values, it interprets the value as 254. Therefore, statement print(0b11111110) outputs 254 in the screen.
It is represented by & and can be applied on two operands. The AND operator compares two bits and returns 1 if both bits are 1, otherwise returns 0.
In the above program, the statement let result = xBits & yBits combines the bits of two operands xBits and yBits. It returns 1 it both of the bits are 1 otherwise it returns 0.
String(value , radix: ) initializer is used to represent number in different number system. If we supply radix value 2. It converts the number to binary number system. Similarly, we can use 16 for hex and 10 for decimal.
The statement print("Binary:",String(result, radix: 2)) outputs Binary: 10000011 in the screen. 10000011 is equivalent to 131 in decimal,the statement print(result) outputs 131 in the console.
It is represented as | and can be applied on two operands. The bitwise OR operator compares two bits and generates a result of 1 if one or more of its inputs are 1 otherwise 0.
In the above program, the statement let result = xBits | yBits combines the bits of two constants xBits and yBits. It returns 1 if any of the bits are 1 otherwise it returns 0.
The statement print("Binary:",String(result, radix: 2)) outputs Binary: 11111111 in the screen. Since, 11111111is equivalent to 255 in decimal, the statement print(result) outputs 255 in the screen.
It is represented as ^ and can be applied on two operands. The XOR operator compares two bits and generates a result of 1 if exactly one of its inputs are 1 otherwise it returns 0.
In the above program, the statement let result = xBits ^ yBits combines the bits of two constants xBits and yBits. It returns 1 if exactly one of the bits is 1 otherwise it returns 0.
The statement print("Binary:",String(result, radix: 2)) outputs Binary: 1111100 (equivalent to 01111100)in the screen. Since, 1111100 is equivalent to 124 in decimal, the statement print(result) outputs 124 in the screen.
This operators are used to move all bits in a number to the left or the right by a certain number of places and can be applied on single operand. It is represented as << or >>.
It causes the bits to be shifted to the left specified by the number followed by <<.
The bit positions that have been vacated by the shift operation are zero-filled.
In the above program, we have used left shift operator. Using << 1 means to shift the bit by 1 to the left. The digits get shifted to the left by one position, and the last digit on the right is filled with a zero.
You can also see the digit that gets shifted "off the end" from left side is lost. It does not wrap around again from the right. Shifting it one bit to the left removes the 1 from the binary and adds 0 in the right to fill the shifted value as well rest of the other bits are shifted towards left position by 1.
This returns 10001000 which is equivalent to 136 in UInt8. Therefore, print(someBits << 1)statement outputs 136 in the screen.
For unsigned numbers, the bit positions that have been vacated by the shift operation are zero-filled.
For signed numbers (numbers that can also be negative), the sign bit is used to fill the vacated bit positions. In other words, if the number is positive, 0 is used, and if the number is negative, 1 is used.
Shifting it to the right by one position halves its value.
In the above program, we have used right shift operator on a unsigned integer. Using >> 1 means to shift the the bit by 1 to the right. The bit positions that have been vacated by the shift operation are always zero-filled on a unsigned integer.
Since, 4 is represented as 00000100 in binary. Shifting it one bit to the right, returns 00000010 which is equivalent to 2 in UInt8. Therefore, print(someBits >> 1) statement outputs 2 in the screen.
In the above program, we have used right shift operator on a unsigned integer. Unlike positive numbers, using >> for negative numbers, 1 is used to fill the vacant place, instead of 0.
Since, -4 is represented as 11111100 in binary. Shifting it one bit to the right and placing 1 in vacant position, returns 11111110 which is equivalent to -2 for Int8 type. Therefore, print(someBits >> 1)statement outputs -2 in the screen.