Math For Programmers

This course covers the maths behind how your computer stores and manipulates data. You'll learn how to read binary and hexadecimal, how both integers and floating point numbers are stored and the limitations of using them. Advice on best practices and how to work effectively with boolean values and bitwise operators.
Course info
Rating
(422)
Level
Intermediate
Updated
Aug 26, 2013
Duration
5h 16m
Table of contents
Math for Programmers
Types of Data
Working in Binary
Integers
Floating Point Numbers
Logic, Booleans and Bitwise Operations
Errors and Accuracy
Description
Course info
Rating
(422)
Level
Intermediate
Updated
Aug 26, 2013
Duration
5h 16m
Description

Have you ever wondered exactly why displaying the contents of memory often gives strange-looking numbers like 0x38FF that contain letters? Or puzzled over a time when your code added two floating point numbers but the result wasn't quite correct? If so, then this is the course for you. It aims to teach you the mathematics behind how computers store and manipulate numbers and booleans. You'll learn, amongst other things, how to read binary and hexadecimal numbers, and why floating point numbers are almost always stored with small errors and how to estimate how big those errors are. You'll find out how to simplify complex logical expressions, or how to use logical operators and bitwise flags to very efficiently store and manipulate sets of true/false properties. The course also features lots of advice on best practices for working with numerical and boolean data. Underpinning all this, you'll learn how to see your data the way a computer sees it. And that, when your job involves writing or debugging code that manipulates millions of bytes of data, is a very useful skill to have.

About the author
About the author

Simon is a senior developer with over 20 years programming experience, particularly covering C++ and the .NET Framework. He believes knowledge is to be shared and has written or co-written more than a dozen books for professional programmers.

More from the author
C# Concurrent Collections
Advanced
3h 32m
May 15, 2015
C# Equality and Comparisons
Intermediate
4h 51m
Sep 10, 2014
C# Collections Fundamentals
Intermediate
5h 56m
Apr 8, 2014
Section Introduction Transcripts
Section Introduction Transcripts

Math for Programmers
Hello, and welcome to this Pluralsight's course Math for Programmers. I'm Simon Robinson. You can find me at my blog, TechieSimon. com. There's also a link to contact me there, or I'm on Twitter @TechieSimon. Now, the aim of this course is to give you a solid understanding of how your computer, and, therefore, your code, manipulates and stores numerical and logical data, and to make sure you have sufficient background in maths to write good and correct code, wherever mathematical or logical operations are concerned. And, since just about any untrivial program will use numbers and logic, that's potentially quite a wide scope.

Types of Data
This module is the first of the two background modules. When we are programming, we use data of lots of different types, characters, strings, Booleans, numbers, and then to make things more complicated, we compose these basic types into structs and classes too. However to a computer, at the most fundamental level, there is only one data type, the set of bits in memory. In this module, we'll explore this concept and learn about the structure of the data in memory. In particular we'll cover how data is stored. We'll examine what data looks like once it's in your computer's memory, and how this is affected when it's moved to registers in order to do operations on it. Numbers of values will ask how many unique values can be stored in a given bit of memory. The answer will naturally lead us to revise the mathematical concept of powers. Interpreting bit patterns in this section we'll see that the meaning of data comes from how the bit patterns are interpreted rather than the bit patterns themselves. And mapping high-level data types will finish the module by working through the data types available in C# to see how all data ultimately becomes a Boolean, an integer, or a floating points number. This is by the way a fairly elementary module, so if you do already understand how data is represented as bits, which a lot of you will do, you may prefer to just skip ahead to the next module on binary arithmetic.

Working in Binary
Working in Binary. As you're no doubt aware, computers store numbers in binary. This affects almost every aspect of manipulating data and means that understanding binary arithmetic is an essential prerequisite to being able to deal effectively at a low level with numbers, and indeed with almost any data stored in a computer. The point of this module is to provide that understanding, and to make sure that you're comfortable looking at numbers the way a computer sees them. Specifically this module will cover number bases. I'll explain the concept of number bases and explain why computers count in twos. I'll also show you how to understand numbers using the number line. Binary arithmetic, this part of the course aims to familiarize you with the binary form of numbers that computers use, and also with hexadecimal and octal. Special numbers will compare how different bases impact which numbers are round and therefore considered special. We'll also examine how this affects units used in computing. And finally converting between bases, the aim of this part of the course is to go over how to convert between the different number bases.

Integers
Integers. This module is about how integers are represented on your computer, and the consequences for you on how you should use them. We'll start off by covering Overflow. We'll see how overflows occur and how you can deal with them. Then, we'll look at Negative Numbers. We'll learn how negative integers are represented using 2's complement. Next we'll learn about how Sign Extension is carried out in order to change the word length of integers. Integer Division, we'll have a look in detail at how division works for integers, and we'll focus on some division 'Gotchas' that you need to be aware of, and we'll also compare it with shift operators. And finally, we'll examine some Best Practices for working with integers and numbers. We won't, by the way, be covering most bitwise operators in this module. We'll leave that for the module on logic, later in this course.

Floating Point Numbers
Floating Point Numbers This module covers floating point numbers and aims to give you the background you need to write code to carry out problem-free floating point operations. We'll start with some essential background looking at the different types of numbers that exist that you may need to store and we'll see why many of these numbers can't be stored exactly. Then we'll look at different ways that fractions can be represented on your computer, before learning how floating points are actually stored, and we'll consider issues like subnormal numbers and dealing with infinity. Next we'll look at the difficulties involved with comparing whether floating point values are equal and we'll examine the possible workarounds, before considering Best Practices, including use of decimal types. Now before I start, I should emphasize one thing - it's crucial to understand when dealing with floating point numbers that they are not simply integers with fractions tacked on. As far as your computer is concerned, they constitute a completely different type of number. The rules of dealing with them are different; for example, when you work with integers you depend on things like the fact that you can represent numbers exactly and you can easily compare numbers for equality, but you can't rely on those assumptions for floating points, and the representation is different. That all significantly changes how you should use floating point numbers.

Logic, Booleans and Bitwise Operations
Logic, Booleans and Bitwise Operators In this module, we're going to look at the math around the final one of the three fundamental types we've been looking at, the Boolean. This requires us to delve a bit into logic theory. Logic theory is a very deep topic, but we only need to skim the surface to get what we need for most programming with Booleans. The really interesting application is for bitwise operations. So I'm going to try and get through the basic logic as quickly as possible so we can focus on that area. The topics we'll cover are Logical Operators; I'll explain the AND, OR, XOR, and NOT operators. Simplifying Expressions, I'll show you how to simplify logical expressions. A very important tool to assist with this is the truth table, so I'll show you how that works. Bitwise Operators; I'll show you how these work and look in detail at the two situations where bitwise operators are at their most useful - flags and masking. And finally as usual, we'll look at a couple of Best Practices.

Errors and Accuracy
Errors and Accuracy This final module is about being aware of how big the errors that can build up in your calculations can be. We'll start by looking in detail at the different ways that you can round numbers. Next we'll examine precisely how large an error might be involved when you store a number as a 32-bit or 64-bit floating point. Along the way, we'll understand the difference between absolute and relative errors. Then we'll consider the ways that errors can grow and propagate when you perform arithmetic operations. And finally, we'll draw some Best Practices out of the material.