#1 Introduction to data types – Boolean

Reading Time: 4 minutes

The boolean data type for beginners – In the first official part of this series about data types for beginners, we will look at the simplest data type in computer science. The Boolean.

Data types for beginners

Series Content

  • Introduction (view)
  • Boolean
  • Integers (view)
  • Floating Point Numbers
  • Characters and Strings
  • Complex Data Types

The boolean data type for beginners

Post Content

  1. Definition
  2. Operations
  3. Storage
  4. Usage
  5. Conclusion

1. Definition

The Boolean data type can only hold one of two values. These values are either 1 or 0 or in other words, true or false. Its purpose is to represent the two truth values of logic. This means that we can use logical operators on Boolean data. This simple data type is named after George Boole, the first man to define an algebraic system of logic, which is fundamental for most things in computer science nowadays. Booleans are used most likely in conditional statements (e.g. if/else, switch, etc.) but can be used in various ways.

2. Operations

In the introductory part of this series, we learned that there are different types of operations. We learned that there are unary (1 input), binary (2 inputs) and ternary (3 inputs) operations. We can use all of the 6 operations from Boolean algebra in programming but the 3 secondary operations are just a composition of the 3 basic operations. Therefore we don’t have 6 (mostly 4-5) operators to use in our code.

NOT

The first operation I want to talk about is a unary one. It works similar to the “-“ operator for numbers as it simply negates its input value, i.e. not true is equal to false and vice versa. In most programming language the not operator is represented by an “!” or the literal word “not”.

Code example in Java:

!true //returns false
!false //returns true

AND

The second basic operation we can perform on Booleans is the “and” operation. In math, this is called a conjunction. This operation is binary. It “combines” both of its inputs and returns “true” or 1 only if both inputs are true themselves. In programming, the and operator is often represented by a “&&” or the literal word “and”.

Code example in Java:

true && false //returns false
true && true //returns true
false && false //returns false

OR

The last basic operation is also binary. It’s called disjunction in math and returns true if at least one input is true. In many languages, the or operator is represented as a “||” or the literal word “or”.

Code example in Java:

true || false //returns true
true || true //returns true
false || false //returns false

Secondary Operations

Like I mentioned earlier, there are three more operations we can use on Boolean values. These don’t have to have their own operators in programming languages as they can be expressed as a combination of the three basic operations.

The first and probably most known secondary operation is the “exclusive or” or XOR. It is binary and returns true if exactly one of its two inputs is true. In some languages, we can use an operator for the XOR operation. E.g. Java has the “^” operator (be careful with using it tho as it’s a bitwise operator).

Code example in Java:

true ^ false //returns true
true ^ true //returns false
false ^ false //returns false

We can also express the exclusive or as a combination of basic operators (Example in Java):

//let x and y be some boolean

(x || y) && !(x && y)

//or:

(x && !y) || (!x && y)

The second operation is called equivalence and is also binary. It returns true if both inputs have the same value. You may also refer to this as “equal to” or something similar. In most languages, we can use the “==” operator.

Code example in Java:

true == false //returns false
true == true //returns true
false == false //returns true

We could also express this as a combination of basic operations (Example in Java):

// let x and y be some boolean

(x && y) || (!x && !y)

The last secondary operation is the material implication. This operation is more of a theoretical kind of thing in math and has its place in there for sure, but in my experience, it is a little bit more advanced and you will most likely never use it knowingly in any of your programs, so I decided to not include it here in detail, because this should be about the boolean data type for beginners. If you’re interested anyway check out Wikipedia.

3. Storage

We learned that Booleans can only have one of two values. Therefore it would be obvious to store a Boolean as a single bit in computer memory. For those of you that don’t know: A bit is the smallest possible storage unit on a computer and can hold only one of two values; either 0 or LOW (for low currency) or 1 or HIGH (for high currency).

It would make sense to store Boolean data in only one bit but this is in most modern programming languages not the case. In most cases, Booleans are stored in one byte (1 Byte = 8 Bit). This may seem like a waste of storage, which is technically true but it has something to do with how computers (more precisely CPU’s) read input data. A processing unit on modern computers is constructed in a way so that it reads at least one byte or more in one go. Therefore most programming languages store their Booleans in one byte. If all 8 bits are 0, the value of the Boolean is false. If it’s higher than 0, i.e. at least one bit is 1, it has the value true.

4. Usage

As I mentioned in the beginning, Booleans are most likely used in control structures such as an if/else statement. That’s because those control structures need conditions that can either be true or false. If you ever worked with an if-statement or a loop you probably know what I mean.

5. Conclusion

Boolean is the simplest data type in the world of computer science but it has way more depth to it than I thought when I first learned to program. The fundamental mathematical concepts of the Boolean data type are essential for modern computer science and can be very irritating at times. Nevertheless, I hope that you learned something new by reading this article and had some fun doing it. If you did, please let me know as I put much effort into writing this post.

Please give me some feedback on my work and let me know how you liked it in the comments or on social media.

Always keep learning and have a nice day,

Fred

2 comments

Leave a Reply

Your email address will not be published. Required fields are marked *