Bit Operations

Besides this, we use integers, doubles or floats types, all of them have their own bit representations. That bit representation causes many problems when you want to keep very small or very big float or double for example not precious calculation.

Let’s keep closed to see what basic bit operations we have in Swift. There are Negative (~), OR (|), AND (&), XOR (^), Left Shift (<<) and Right Shift (>>).

We have two UInt8 variables, so it means each of them is saved using 8 bits.

Maximumg value: 255, bit notation: 11111111.
Miminum value: 0, bit notation: 00000000.

let numberOne: UInt8 = 25
let numberTwo: UInt8 = 28

print(“Number one binary:”, String(numberOne, radix: 2))
print(“Number two binary:”, String(numberTwo, radix: 2))


Number one binary: 11001
Number two binary: 11100

Remember: if the number in bit notation starts from zeros they will not be printed but they are omitted but they are still there.

Negative Operation (~)

Switch bits from 0 into 1 and from 1 into 0. Remeber about zeros at the beginning.

Numer one: 00011001
Result:          11100110

let negative = ~numberOne
print(“Number one negative binary:”, String(negative, radix: 2))
print(“Number one negative decimal:”, String(negative, radix: 10))


Number one negative binary: 11100110
Number one negative decimal: 230

OR Operation (|)

The resulting bit will have 1 if any of the bit was 1, but they do not sum up.

Number one: 11001
Number two: 11100
Result:             11101

let OR = numberOne | numberTwo
print(“OR binary:”, String(OR, radix: 2))
print(“OR decimal:”, String(OR, radix: 10))


OR binary: 11101
OR decimal: 29

AND Operation (&)

The resulting bit will have 1 if both component bits had 1.

Number one: 11001
Number two: 11100
Result:             11001

let AND = numberOne & numberTwo
print(“AND binary:”, String(AND, radix: 2))
print(“AND decimal:”, String(AND, radix: 10))


AND binary: 11000
AND decimal: 24

XOR Operation (^)

The resulting bit is the remind after divide by 2. Sum up bits and the divide by 2, the remind is the result. In this case, it is the same as if only one bit is 1 and one is 0.

Number one: 11001
Number two: 11100
Result:             00101

let XOR = numberOne ^ numberTwo
print(“XOR binary:”, String(XOR, radix: 2))
print(“XOR decimal:”, String(XOR, radix: 10))


XOR binary: 101
XOR decimal: 5

Left Shift (<<)

Each bit is moved to the left, a value from the first bit will be in place of second, a value from second will be in place of third and so on. It is the same as you multiply the number by 2.

Number one: 11001
Result:           110010

let shiftLeft = numberOne << 1
print(“shiftLeft binary:”, String(shiftLeft, radix: 2))
print(“shiftLeft decimal:”, String(shiftLeft, radix: 10))


shiftLeft binary: 110010
shiftLeft decimal: 50

Rigth Shift (>>)

It works the same as move to left but this time each bit is moved to the right. It is same as devide by 2.

Number one: 11001
Result:                    11

let shiftRight = numberTwo >> 3
print(“shiftRight binary:”, String(shiftRight, radix: 2))
print(“shiftRight decimal:”, String(shiftRight, radix: 10))


shiftRight binary: 11
shiftRightdecimal: 3