Skip to content
Tutorials Center
My learning

Topics

Lesson 89 of 134 ยท Text, Files and Time

Bits and Bytes

Learn Kotlin bit manipulation - shl, shr, ushr, and, or, xor, inv, masks and flags, ByteArray, UByte, HexFormat, toString(radix), Base64 and encoding text to bytes - with runnable examples.

Everything in a computer is bits: the Int you add, the text you print, the file you save. Most of the time you never see them, because the language hides them behind numbers and strings. But sometimes you need to look underneath: to pack several yes/no settings into one number, to read a binary file format, to compute a checksum, or to send text over a network as bytes. That is what this lesson is about.

Kotlin has no &, | or << operators. Instead, the bitwise operations are infix functions with names: and, or, xor, inv, shl, shr, ushr. They read like words and have a lower precedence than arithmetic, which surprises people coming from Java or C. After the operations we look at ByteArray, unsigned types, hexadecimal output with HexFormat, and Base64.

Numbers in binary

Int has 32 bits and Long has 64. Write binary literals with 0b and hexadecimal literals with 0x; underscores make long literals readable. toString(radix = 2) shows a number in binary, and padStart lines the bits up.

Kotlin Binary and hexadecimal literals

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with readln()), it gets the next line from this box.

The last line is worth pausing on. toString(2) of a negative number prints a minus sign followed by the magnitude (-1), so the length is 2. To see the real bit pattern of a negative number, use Integer.toBinaryString(-1) from Java, or toUInt() first (see below). Internally, the JVM stores negative integers in two's complement: -1 is 32 ones in a row.

The bitwise functions

Function Meaning Example
a and b 1 where both bits are 1 0b1100 and 0b1010 is 0b1000
a or b 1 where either bit is 1 0b1100 or 0b1010 is 0b1110
a xor b 1 where the bits differ 0b1100 xor 0b1010 is 0b0110
a.inv() flips every bit 0.inv() is -1
a shl n shifts left, filling with zeros 1 shl 4 is 16
a shr n shifts right, keeping the sign -16 shr 2 is -4
a ushr n shifts right, filling with zeros -1 ushr 28 is 15
Kotlin The bitwise functions

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with readln()), it gets the next line from this box.

Shifting is multiplication and division by powers of two: n shl 3 is n * 8, and n shr 1 is n / 2 rounded toward negative infinity (for negative numbers, ordinary / rounds toward zero, so the results differ). 1 shl 32 surprises many people: on Int the shift count is reduced modulo 32, so the result is 1 shl 0, which is 1. For bigger shifts use Long: 1L shl 40.

Precedence trap: infix functions bind weaker than + and -. So 1 shl 2 + 1 means 1 shl 3 and gives 8, not 5. They bind tighter than ==, != and &&, so flags and MASK == 0 means (flags and MASK) == 0, which is usually what you want. Do not memorize the table; add parentheses whenever you mix bitwise functions with other operators.

Masks and flags

The most common practical use of bits is a flag set: each bit is one yes/no option, and a single Int stores up to 32 of them. A mask is a number with the bits you care about set to 1.

Kotlin Permissions as bit flags

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with readln()), it gets the next line from this box.

Notice that p and READ != 0 in describe works without parentheses, because infix calls bind tighter than !=. It is still kinder to the reader to write (p and READ) != 0.

The four recipes every programmer should know:

  • Set a bit: value or mask
  • Clear a bit: value and mask.inv()
  • Toggle a bit: value xor mask
  • Test a bit: (value and mask) != 0

For named sets of options in real code, an enum class with EnumSet is usually clearer (see enum classes). Raw bit flags are for file formats, protocols and performance-critical code.

Bytes and ByteArray

A Byte is 8 bits, and in Kotlin it is signed: its range is -128..127. That is a classic source of bugs. The value 0xFF does not fit in a Byte literal, and a byte read from a file that holds 0xFF appears as -1. To see the unsigned value, mask it with and 0xFF, which converts it to an Int first.

Kotlin Signed bytes and the 0xFF mask

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with readln()), it gets the next line from this box.

println(data) on an array prints an object address, not the contents; always use contentToString() for arrays. Kotlin never converts between numeric types implicitly, so every step from Byte to Int is a call such as toInt().

Unsigned types

Kotlin also offers UByte, UShort, UInt and ULong. They store the same bits but interpret them as non-negative numbers, so a UByte runs from 0 to 255. Literals end in u: 200u, 255uL. They are a good fit for binary data.

Kotlin UByte and UInt

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with readln()), it gets the next line from this box.

Unsigned arithmetic wraps silently at the top, just like signed arithmetic does. If you need overflow detection, use Math.addExact, or switch to Long or BigInteger (see numbers).

Hexadecimal and HexFormat

Two hex digits describe exactly one byte, which is why hex is the standard way to display binary data. The standard library has HexFormat for this (stable in current Kotlin; older versions needed an opt-in). The functions toHexString() and hexToByteArray() convert in both directions.

Kotlin Bytes to hex and back

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with readln()), it gets the next line from this box.

Note that 255.toHexString() on an Int prints all four bytes (000000ff), while the Byte version prints ff. Hex output is the classic way to print checksums, colors (0xFF8800), and network addresses.

Text is bytes too

A String is a sequence of characters; a file or a network socket carries bytes. Encoding turns characters into bytes, decoding reverses it. Always name the encoding, and use UTF-8 unless a format says otherwise. In UTF-8, ASCII letters take one byte, and other characters take two to four.

Kotlin Encoding and decoding with UTF-8

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with readln()), it gets the next line from this box.

The replacement character you see for the cut character is how decodeToString() reports invalid input by default. Pass throwOnInvalidSequence = true if you prefer an exception.

Base64

Base64 turns arbitrary bytes into plain ASCII letters, digits, + and /, so that binary data can travel through text-only channels such as JSON, email or URLs. It makes data about a third larger. Kotlin has kotlin.io.encoding.Base64 (stable in current Kotlin), and the JDK offers java.util.Base64.

Kotlin Base64 round trip

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with readln()), it gets the next line from this box.

Base64 is an encoding, not encryption. Anyone can decode it. Never use it to "hide" passwords or secrets.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What does 1 shl 3 evaluate to?

    Shifting left by 3 multiplies by 2^3, so 1 shl 3 is 8 (binary 1000).
  2. 2. Which expression turns OFF the bit given by mask in value?

    AND with the inverted mask keeps every bit except the ones in the mask. xor toggles instead of clearing, and or sets bits.
  3. 3. A Byte holds the bits 11111111. What does Kotlin print, and how do you get 255?

    Byte is signed, so all ones is -1. Converting to Int and masking with 0xFF keeps only the low 8 bits, giving 255.
  4. 4. What is the difference between shr and ushr?

    shr is the arithmetic shift and copies the sign bit into the vacated positions. ushr is the logical shift and always shifts in zeros.
  5. 5. Is Base64 a safe way to hide a password?

    Base64 only changes the representation of the bytes. Anyone can decode it without a key.

Summary

  • Kotlin spells the bitwise operators as functions: and, or, xor, inv, shl, shr, ushr.
  • Infix functions bind weaker than + and -; put parentheses around mixed expressions.
  • Set a bit with or, clear with and mask.inv(), toggle with xor, test with (v and mask) != 0.
  • Byte is signed; use toInt() and 0xFF or the unsigned types UByte and UInt for unsigned views.
  • toHexString() and hexToByteArray() convert bytes to hex text and back; use contentToString() to print arrays.
  • encodeToByteArray() and decodeToString() convert text to UTF-8 bytes and back; Base64 is encoding, not encryption.

Next: Working with JSON Text