Skip to main content
Be Bitwise

Text, Characters & Booleans

Variables & Data Types · Coding Fundamentals track

6 min read

In this module

Beyond Numbers

Programs deal with far more than arithmetic. Text, true/false decisions, and individual characters are foundational data types that every programmer needs to understand. Like numeric types, each maps to specific bit patterns in memory.

Characters

A character represents a single symbol: a letter, digit, punctuation mark, or even an emoji. A computer can't store a letter directly though. It stores a number that maps to a letter via an agreed-upon encoding.

The oldest widely used encoding is ASCII (American Standard Code for Information Interchange), which maps 128 characters to the numbers 0 through 127. The letter A is 65, B is 66, a space is 32, and so on. ASCII fits in 7 bits and only covers English characters.

Unicode is the modern standard that aims to represent every writing system on Earth. It assigns a unique code point (a number) to over 150,000 characters, from Latin and Cyrillic to Chinese, Arabic, emoji, and ancient scripts.

The most common encoding for Unicode is UTF-8, which uses a variable-length scheme. ASCII characters take 1 byte, most European characters take 2, CJK characters take 3, and characters beyond the Basic Multilingual Plane (including emoji) take 4.

The length lives in the bit patterns themselves. A byte starting 0 is a whole ASCII character on its own. A byte starting 110 opens a two-byte character, 1110 a three-byte one and 11110 a four-byte one, and every byte that continues a character starts 10. The code point's own bits fill the x slots:

CharacterCode pointUTF-8 bytesPattern
AU+0041410xxxxxxx
éU+00E9C3 A9110xxxxx 10xxxxxx
😀U+1F600F0 9F 98 8011110xxx 10xxxxxx 10xxxxxx 10xxxxxx

Here's é up close, with both of its bytes in one 16-bit row. The first byte is 11000011: the 110 marker, then 00011. The second is 10101001: the 10 marker, then 101001. Glue the payload bits together and you get 00011101001, which is 233, or 0xE9, the code point.

215
214
213
212
211
210
29
28
27
26
25
24
23
22
21
20
Decimal50089
Hex0xC3A9
Binary1100001110101001

Flip any of the marker bits and you've made invalid UTF-8. Decoders are supposed to reject that, and ones that don't have been a source of security bugs.

Key Concept

Characters are numbers in disguise. An encoding like ASCII or UTF-8 defines the mapping between numbers and symbols. Encodings matter because the same byte sequence can mean different things under different encodings.

Strings

A string is a sequence of characters. In most languages you create strings by wrapping text in quotes:

text
let greeting = "Hello, world!"
let empty = ""
let multiline = "Line one\nLine two"

Under the hood, a string is stored as an array of bytes (or code units) in memory. Different languages implement strings differently:

  • C stores strings as null-terminated arrays of bytes: a sequence of characters followed by a zero byte (\0) that marks the end.
  • Many modern languages store strings as an object holding a pointer to the character data, a length field, and sometimes a capacity field.

Before you run this, predict what strlen and sizeof will report for each string. The second one has four characters, but how many bytes?

Try itsizeof counts the terminating zero byte, strlen doesn't, and neither counts characters once UTF-8 gets involved.
C (GCC 9.2.0)

é is a single character but two bytes in UTF-8 (c3 a9), so strlen reports 5. In C, a string's length is a byte count, and counting characters means decoding the UTF-8 yourself.

What goes wrong with char word[5] = "hello";?

C allows it: the array has exactly enough room for the five letters, so the compiler quietly drops the \0. Nothing marks the end any more, and printf("%s", word) or strlen(word) keeps reading past the array until it happens to hit a zero byte somewhere else in memory. Leave the size out (char word[] = "hello";) and the compiler makes it 6.

Strings support operations like concatenation (joining two strings together), slicing (extracting a substring), searching (finding a character or pattern), and comparison (checking if two strings are equal or working out alphabetical order).

text
let first = "Hello"
let second = "World"
let combined = first + ", " + second + "!"
// combined is "Hello, World!"

String Immutability

In many languages, strings are immutable. Once created, their contents can't be changed. If you want a modified version, you build a new string. This design simplifies memory management and makes strings safe to share between different parts of a program.

text
let s = "hello"
// s[0] = "H" - ERROR in languages with immutable strings
let s2 = "H" + s[1..] // create a new string instead

Booleans

A boolean is the simplest data type going. It holds one of exactly two values, true or false. Named after the mathematician George Boole, booleans underpin all decision-making in programs.

text
let is_logged_in = true
let has_permission = false

You've already met the underlying idea in the logic gates module: AND, OR, NOT operations on single bits. Booleans bring that same logic up into the programming world.

Boolean Expressions

Booleans are most often produced by comparison operators:

text
5 > 3 // true
10 == 20 // false
"abc" != "xyz" // true
age >= 18 // depends on the value of age

And combined with logical operators:

text
is_admin OR has_special_pass // true if either is true
is_logged_in AND has_permission // true only if both are true
NOT is_banned // flips the value

These expressions drive control flow: the if/else decisions and loop conditions you'll meet in the next module.

Truthiness and Falsiness

Some languages blur the line between booleans and other types. In these languages, non-boolean values get treated as "truthy" or "falsy" in boolean contexts:

  • Falsy values typically include 0, 0.0, empty string "", null/nil/None, and false.
  • Truthy values are everything else.
text
if (username) {
// runs if username is not empty, null, or zero
}

Convenient, but also a fertile source of subtle bugs. Strongly-typed languages often demand explicit boolean values in conditions, trading convenience for clarity.

The Null Problem

Most languages have a concept of null (or nil, None, undefined), a special value meaning "no value" or "nothing." It comes out when a variable exists but doesn't currently point to any meaningful data.

Null is useful and dangerous in equal measure. Try to use a null value as if it were real (calling a method on it, indexing into it) and you get a null reference error, one of the most common bugs in all of software. Some modern languages (Rust and Kotlin among them) have type systems that force you to explicitly handle the possibility of null, eliminating entire categories of bugs at compile time.

Get a feel for how text, booleans, and null values work, and for the traps they set, and you're well on the way to writing programs that behave themselves.