C++ — Negative Numbers & Interactions w/ Bits

Jake Cutter
Technology Hits
Published in
3 min readDec 15, 2021

For a computer, it is operated off of ones and zeroes. This is pretty common knowledge. But what are the ones and zeroes? Why are they important? How does the computer read the numbers and why does it matter?

Photo by John Schnobrich on Unsplash

WHAT ARE BITS?

A bit is an electron that passes through the computer. Every 8 bits total 1 byte. This is the fundamental idea of where binary comes from. Your computer reads the 8 bits and determines what it is seeing. In C++, we have different types of data. The data is made of things like strings, integers, doubles, or floats, etc. as we’ve previously discussed.

HOW DOES A COMPUTER READ DATA?

A computer reads the data from the coding language directly to binary digits. For example, 0000_0001.

Often in coding languages, we don’t code directly in binary because of further technological advances. We have hexadecimal, decimal, and others that coding languages use now because they are simpler and quicker to work with than individual bits of data.

However, at the computer level below the coding language, all it sees is binary. It reads each individual electron as the data input.

WHAT KIND OF DATA IS THERE?

There are different kinds of data that I have discussed here. To summarize, modern coding languages do the grunt work for us by having the underlying workings of the bits as the data values.

The data values include strings, integers, doubles, floats, classes, and loops. Each of these has different values of bits on them, which is called their data size. This becomes important later on when you are working through large amounts of data because if you need more data than you currently have your leading bits fall off, or the computer no longer reads them. This becomes more important in high-impact code.

NEGATIVE NUMBERS?

In order for your computer to read a negative number, we have to use something called Two’s compliment. Two’s complement is where you flip all of the bits and add one. This essentially creates the same number but has a leading one. A leading one means that the left-most digit is a one, which means negative. It's the most significant bit, MSB.

Example:

2 = [0000_0011] -> [1111_1100] + 1 -> [1111_1101] = -2

Reasoning:

By flipping the bits you get something called One’s Compliment. By adding the one you are able to go back to the same value number with a 1 as the MSB.

With Two’s Compliment in mind

Along with data type we have different formations of ways we can represent the number of bits. We have decimal, hexadecimal, octal, or binary. We can also represent it as any other number by conversion. The most common is hexadecimal. Hexadecimal uses 16 bits for one value.

Its values range from 0–9, then up to 15 as letters.

A -10, B-11 , C-12, D-13, E-14, F-15

Example:

Decimal: 22

Hexadecimal:

  1. Convert to binary: 0000_0000_0001_0110
  2. Convert to Hex:0016

Reasoning:

Once you convert your number to binary, Hex is convenient for conversion because every four bits adds up to the 15 total Hex values. So for each four binary bits, you can get a Hex value. For example, the 0110 is equal to 6.

Two’s Compliment Example:

Decimal: -12

12 -> [0000_0000_0000_1100]

-12 -> [1111_1111_1111_0100]

Hexadecimal: FFF4

Reasoning:

In order to convert to Hex, you need to convert to binary. Once you have a binary value, you can apply the Hex concept.

Afterthought:

When working with values, it is best to work with the positive version of a number. If you have a negative value then your answers can be skewed and can be incorrectly calculated.

Additionally, there is something called Masking which I will cover in a later article that can move a number around to section off part of a value to find a specific bit. This comes in handy when we need to use the test, set, and clear concepts.

Thanks for reading!

Citations:

Marz, Stephen. Professor. University of Tennessee. Knoxville, Tennessee. 2021

--

--