What is is_binary in Elixir?
What are binaries?
To understand binaries, you need to know what bit-strings are.
Bit-strings represent a contiguous sequence of bits in memory. The syntax used to denote a bit-string is <<>>. ::n can be used to specify the number of bits used to represent a single
A binary is a bit-string whose number of bits is divisible by 8.
To check whether the given bit-sequence is a binary or not, we can use the is_binary method:
IO.puts(is_bitstring("edpresso"))# output: trueIO.puts(is_bitstring("edpresso"))#output: trueIO.puts(is_bitstring(<<64::16, 128::72, 256::80>>))#output: trueIO.puts(is_binary(<<64::16, 128::72, 256::80>>))#output: trueIO.puts(is_bitstring(<<64::17, 128::73, 256::81>>))#output: trueIO.puts(is_binary(<<64::17, 128::73, 256::81>>))#output: false
Observation
- A string is a UTF-8 encoded binary, where the code point for each character is encoded using 1 to 4 bytes; thus, every string is a binary.
- When the bit-string 64, 128, and 256 is represented using bits that are divisible by 8, it’s a binary.
- Whereas, the bit-string 64, 128, and 256, when represented using bits that are not divisible by 8, is not a binary.
Since all strings are binary, using
is_binarymight be useful when you want to distinguish whether or not the input or return value is a string.
Free Resources
Copyright ©2026 Educative, Inc. All rights reserved