Questions for BitString, Binary, Charlist, and String in Elixir — Part 1: BitString (or bits)

qhwa
2 min readApr 18, 2020

This is the first group of questions of the serial:

part 1. BitString (or bits)
part 2. Binary (or bytes)
part 3. String and Charlist

Here are some questions related to BitString in Elixir, a functional programming language. Check how well you know BitString? 😃

Q: Is <<>> a BitString?
A: Yes, it’s a zero-length BitString.

Q: Is "" a BitString?
A: Yes, it is the same as <<>>.

Q: Is <<0>> a BitString?
A: Yes.

Q: What is the value of byte_size(<<0>>) ?
A: 1.

Q: What’s the value of bit_size(<<0>>) ?
A: 8.

Q: Are <<0::8>> and <<0>> the same? And why?
A: Yes. The segment size for an integer is 8 by default.

Q: Is <<0::size(8)>> an equivalent of <<0::8>>?
A: Yes.

Q: Is 0 (the number zero) a BitStrnig?
A: No.

Q: Is [0] a BitString?
A: No, it’s a list.

Q: Are BitStrings lists of bits?
A: No. Bit sequences of BitStrings are consecutive in memory, lists are not.

Q: Is <<0::1>> a BitString?
A: Yes.

Q: What’s the value of bit_size(<<0::1>>?
A: 1.

Q: Is <<2::1>> a valid term in Elixir?
A: Yes, despite its overflowing.

Q: Is <<2::1>> an equivalent of <<0::1>>?
A: Yes.

Q: Is <<0.0>> an equivalent of <<0>> ?
A: No.

Q: What’s the value of bit_size(<<0.0>>) ? Why?
A: 64.
The default size of a float segment is 64.
In contrast, the default size of an integer segment is 8.

Q: What’s inside <<3.14>>?
A: 64 bits representing the float number 3.14.

In detail, it can be decomposed into three parts:

  • 1 bit for sign
  • 11 bits for exponent
  • 52 bits for mantissa
iex> <<sign::1, exponent::11, friction::bits>> = <<3.14>>
...> sign
0
...> exponent
1024
...> friction
<<145, 235, 133, 30, 184, 81, 15::size(4)>>

And compose them back:

with reference to IEEE_754.

Hopefully, in another post following, we’ll see questions related to Binaries in Elixir. Please stay tuned!

--

--