Signed vs Unsigned Bit Integers: What Does It Mean and What's The Difference? (2024)

Signed vs Unsigned Bit Integers: What Does It Mean and What's The Difference? (1)

Aidi Rivera

Posted on

Signed vs Unsigned Bit Integers: What Does It Mean and What's The Difference? (3) Signed vs Unsigned Bit Integers: What Does It Mean and What's The Difference? (4) Signed vs Unsigned Bit Integers: What Does It Mean and What's The Difference? (5) Signed vs Unsigned Bit Integers: What Does It Mean and What's The Difference? (6) Signed vs Unsigned Bit Integers: What Does It Mean and What's The Difference? (7)

#beginners #codenewbie #computerscience #todayilearned

In my previous blogs, I gave an overview of what it means to work with an 8-bit, 16-bit, 32-bit, etc., number, or binary number, and how you would solve an algorithm problem that requires a certain sized bit integer without the computer science background knowledge to help make sense of it all. This post specifically tackles what exactly it means to have a signed or unsigned binary number. It won't change much the way integers are restricted when solving algorithm sets, but it will change the range you can work with dramatically. Then I'll use the same problem solved previously but accommodated to help solve for a signed binary integer instead of one that isn't.

What Does It Mean?

The biggest difference between a signed and unsigned binary number is that the far left bit is used to denote whether or not the number has a negative sign. The rest of the bits are then used to denote the value normally.

This first bit, the sign bit, is used to denote whether it's positive (with a 0) or negative (with a 1). If you want to get technical, a sign bit of 0 denotes that the number is a non-negative, which means it can equal to the decimal zero or a positive number.

(-/+)26252423222120
01001101

010011012 = +7710

Note: I'm using the X2 notation for binary integers and the X10 notation for decimal integers.

Most importantly, the first bit used to denote sign means that we have one less bit to denote value. So if we have an 8-bit signed integer, the first bit tells us whether it's a negative or not, and the other seven bits will tell us what the actual number is. Because of this, we're technically working with a more limited range of numbers that can be represented; 7 bits can't store numbers as big as 8 bits could.

What's the difference?

Unsigned binary numbers

To review binary numbers, the ones and zeroes act like switches that metaphorically turn powers of 2 on, and then it's added up to create the decimal value. Normally, we'd "mark" a bit value with a one.

Example 1a:
Unsigned 01012 = 510

23222120
0101

Example 2a:
Unsigned 10012 = 910

23222120
1001

Signed binary numbers

Example 1b:
Signed 01012 = +510

(-/+)222120
0101

When a signed binary number is positive or negative it's 'marked' with a 0 or 1 respectively at the first far-left bit, the sign bit. The number above doesn't change at all. It's just more explicitly a positive number.

Example 2b:
Signed 10012 = -710

(-/+)222120
1001

But the above binary number completely changes. And we're now representing a negative!

Negative binary integers

When a binary integer is negative, the zeroes will now act as a "marker", instead of the ones. You would then calculate the negative binary number in the same way you would with a positive or unsigned integer, but using zeroes as markers to turn bit values "on" instead of ones and then adding the negative sign at the end of your calculation.

(-/+)222120
1001

Signed 10012 = -710

Going from an unsigned binary to a signed binary integer changes your end value in a couple of different ways. The first is the more obvious change in value when the first bit is used to denote sign instead of value. You can see between example 2a and 2b above that it means if you had a one at the first bit of your 4-bit integer, you're losing a value of 23 that would've been added to your end value with an unsigned bit, but is now instead used to represent a negative. With a larger bit integer, that could be an extremely larger value that you lose the ability to represent.

Something else that isn't obvious right away is that you calculate a negative binary integer's value starting at 1, not 0. Because the decimal zero is not included in a negatively signed bit integer, we don't start counting at zero as we would when it's a positively signed bit integer.

To explain that quirk let's compare positively and negatively signed integers. Working with a 4-bit integer, if we had four bits with a value of zero, the number would equal to 0. That's the lowest value we can have. Because a non-negative signed bit means we can have a positive integer, or a 0.

0000= 010

A 4-bit negative integer of four bits of one values (the ones now being the "off switch"), the number would not equal 0, but -1. Which makes sense, since that's the highest decimal number we can represent while still having a negative.

1111= -110

But that means, when we're adding up our values to get our final decimal number, we start our counting from 1, not from 0.

2x(-/+)222120Total
bit-value0110
2x x bit-val =+420= 610

So even if I were to perfectly flip the "switches" from the positively signed binary number above into its negative counterpart, it would not perfectly switch to its negative decimal counterpart value in the way one might expect:

2x(-/+)222120Total
bit-value1001
2x x bit-val =-42= -710

Why??

Because we're adding starting with a value of 1! And we're adding up the values that are represented in our bits before adding a negative sign at the very end of our calculation.

Here's a visual comparison of the decimal and binary equivalents that show how a 0 signed bit integer is the decimal 010 or larger, while a 1 signed bit integer is decimal -110 or smaller.

Signed vs Unsigned Bit Integers: What Does It Mean and What's The Difference? (8)

Alternatively:

Another way to calculate the negative is to keep using the ones as 'markers' and use the sign bit as a marker for the value at its corresponding power of two at a negative value. This also illustrates a different way to understand what's going on in binary negative representations.

2x(-/+)23222120Total
bit-value1001
2x x bit-val =-8001= -710

This way of calculating the decimal value might be a little easier when working with smaller decimal numbers, but then becomes a little more complicated to do some mental math when you're working with bigger decimal numbers:

2x(-/+)2726252423222120Total
bit-value11110010
2x x bit-val =-1286432160021= -1410

Thankfully, there aren't a lot of situations I can think of where you'd have to interpret between the two without a calculator handy!

Finding the max and min with signed binary numbers

The range of positive decimal numbers that can be stored in any sized bit integer is shortened by the fact that the first bit is used to denote sign. This means that, in the case of a 32-bit signed integer, we are actually working with 31 value bits instead of 32, and that last bit could have stored an exponentially bigger integer. In fact, this completely halves the range of positive integers we can work with compared to a 32-bit unsigned integer. That one extra bit would have doubled our max possible integer, and without it, we lose the ability to store as many positive integers.

On the other hand, we gain the ability to store a bunch of negative integers that we couldn't have before with an unsigned bit integer. In the end, the size of the range we work with is kept the same, but the range moves to account for being able to store both positive and negative numbers.

Let's look at a 4-bit unsigned vs signed integer. Our range might move, but the amount of integers that can be stored don't actually change.

Signed vs Unsigned Bit Integers: What Does It Mean and What's The Difference? (9)

Because of this loss of a bit, our maximum is calculated by 2bits - 1 - 1, or, if working with 32-bit integers 231 - 1.
I explained why we have to subtract the one last time, which we still have to do since we're including the zero in the range and not subtracting would cause one extra bit to be needed to store that number.

Our minimum in the range is the inverse, -2bits - 1, or, if working with 32-bit integers, -231. We don't subtract one for our minimum range because the zero is not included and we start counting from -1. This gives us that one extra negative number in our range that can be represented.

Seeing the range above might help visualize why there isn't a subtraction from the lower range while there is for the upper range. Zero is included in the green range, but not in the red range of signed bits. We start at -1 and can have the same amount of numbers represented as non-negatives.

********
-8-7-6-5-4-3-2-101234567

Here we have 8 positive integers.

********
-8-7-6-5-4-3-2-101234567

Here we have 8 positive and negative integers. But still only 8 total integers.

The problem

Going back to the problem solved in the last post, this time the solution will involve creating a restricted range for a signed integer.

Given a 32-bit signed integer, reverse digits of an integer.

We know this is a 32-bit integer with 32 zeroes and ones, the very first of which is denoting the sign.
Working with 31 bits that could represent the value of the number, the biggest positive binary integer we could have would be 31 ones after the first, sign bit of zero, which gives us a positive sign.

This means the largest decimal number we could deal with would be 231 - 1, or 2,147,483,647

The largest negative binary integer (and by largest I mean smallest?) would be 31 zeroes with the sign bit being a one, telling us it's negative.

This means the smallest decimal number we could deal with would be -231 or -2,147,483,648

The solution

const reverseInteger = (x) => { let reversed = ""; strx = x.toString(); for(let num of strx){ reversed = num + reversed } reversed = parseInt(reversed, 10); if(x<0){ reversed = reversed * -1; } //Setting the range! if (reversed > Math.pow(2, 31) - 1 || reversed < Math.pow(-2, 31)) return 0; return reversed;};

The problem is essentially asking to make sure we don't return a number that can't be stored as a 32-bit signed integer. Here we're skipping how to actually solve this problem and focusing on the range since I've walked through the solution previously.

The line right before the return checks whether the end integer contained in reversed is within range. If reversed is greater than 231 - 1 OR less than -231, it returns 0.

If this were an unsigned 32-bit integer, there would've been a range from 0 to 232-1, or 4,294,967,295. That upper range is twice the range of 231. You can think of that missing "half" of the range that would have stored those positive numbers as being used to store your negative numbers instead. Same-sized range, just different start and endpoints in that range.

Fin!

That finishes my series on binary numbers for the average non-computer science degree holders!

This was a really fun (and frustrating) learning process. I fully expect there to be holes in my overview as there's just way too much to cover without going unnecessarily in-depth.

I want this to be a good jumping-off point for those who want to know the basics so if there's anything that wasn't clear (or I assumed you knew something that you didn't), let me know!

Happy Coding!

Resources:

Reverse Integer LeetCode Problem
Signed Binary Numbers
Decimal to Binary Converter
Signed Numbers - Watson
Rounding Algorithms 101 Redux - EETimes
Bits, Bytes, and Integers - Carnegie Mellon

Signed vs Unsigned Bit Integers: What Does It Mean and What's The Difference? (2024)

FAQs

Signed vs Unsigned Bit Integers: What Does It Mean and What's The Difference? ›

A signed integer is a 32-bit datum that encodes an integer in the range [-2147483648 to 2147483647]. An unsigned integer is a 32-bit datum that encodes a nonnegative integer in the range [0 to 4294967295]. The signed integer is represented in twos complement notation.

What is the difference between signed and unsigned bits? ›

Unsigned binary numbers do not have sign bit, whereas signed binary numbers uses signed bit as well or these can be distinguishable between positive and negative numbers. A signed binary is a specific data type of a signed variable. 1.

What is the difference between 32-bit signed and 32-bit unsigned? ›

A 32-bit signed integer. It has a minimum value of -2,147,483,648 and a maximum value of 2,147,483,647 (inclusive). A 32-bit unsigned integer. It has a minimum value of 0 and a maximum value of 4,294,967,295 (inclusive).

What is the difference between signed and unsigned 16 bit integers? ›

Integer, 16 Bit: Signed Integers ranging from -32768 to +32767. Integer, 16 bit data type is used for numerical tags where variables have the potential for negative or positive values. Integer, 16 Bit Unsigned: Unsigned whole or natural numbers ranging from 0 to +65535.

What is a signed bit integer? ›

Signed integers are numbers with a “+” or “-“ sign. If n bits are used to represent a signed binary integer number, then out of n bits,1 bit will be used to represent a sign of the number and rest (n - 1)bits will be utilized to represent magnitude part of the number itself.

What is the difference between 8-bit signed and 8-bit unsigned? ›

For example, an 8-bit signed binary could hold values from 0-127, both positive and negative (1 bit is used for the sign and 7 bits for the value), while an 8-bit unsigned binary could hold values from 0-255 (nothing distinguishes whether or not the value should be considered positive or negative, though it is commonly ...

Why do we use unsigned int? ›

Unsigned integers are used when we know that the value that we are storing will always be non-negative (zero or positive). Note: it is almost always the case that you could use a regular integer variable in place of an unsigned integer.

What is the difference between signed 16-bit and 32? ›

A 16 bit Signed Integer can house a number from −32,768 to 32,767 Unsigned: 0 to 65,535. A 32 bit Signed Integer can house a number from −2,147,483,648 to 2,147,483,647 Unsigned: 0 to 4,294,967,295.

Is unsigned long 32 or 64 bit? ›

Properties
Type specifierEquivalent typeWidth in bits by data model
C++ standard
unsigned long intunsigned long intat least 32
long longlong long int (C++11)at least 64
long long int
22 more rows

Which is the signed bit? ›

In computer science, the sign bit is a bit in a signed number representation that indicates the sign of a number. Although only signed numeric data types have a sign bit, it is invariably located in the most significant bit position, so the term may be used interchangeably with "most significant bit" in some contexts.

What is the difference between signed and unsigned 32-bit integer? ›

A signed integer is a 32-bit datum that encodes an integer in the range [-2147483648 to 2147483647]. An unsigned integer is a 32-bit datum that encodes a nonnegative integer in the range [0 to 4294967295]. The signed integer is represented in twos complement notation.

What is a 64-bit signed integer? ›

64-bit signed integer type is used to store negativ or pozitiv whole number. 64-bit integer and his value range: from -9223372036854775808 to 9223372036854775807.

What is an 8-bit unsigned integer? ›

An 8-bit unsigned integer has a range of 0 to 255, while an 8-bit signed integer has a range of -128 to 127 - both representing 256 distinct numbers. It is important to note that a computer memory location merely stores a binary pattern.

What is an example of an unsigned integer? ›

The simplest numbers that we want to represent in the machine are the unsigned integers. These are whole numbers without a sign, for example, 0, 1, 2, 3, … The mechanical calculators of yesteryear and the car mileage meter of today both store unsigned integers on what are effectively cogs having ten numbered teeth1.

What is 16-bit signed unsigned integer? ›

16 bit unsigned numbers

There are 65,536 different unsigned 16-bit numbers. The smallest unsigned 16-bit number is 0 and the largest is 65535. For example, 0010,0001,1000,01002 or 0x2184 is 8192+256+128+4 or 8580.

What integer is 24 bit signed? ›

The range of unsigned integers that can be represented in 24 bits is 0 to 16,777,215 (FFFFFF16 in hexadecimal). The range of signed integers that can be represented in 24 bits is −8,388,608 to 8,388,607.

What is the difference between a signed byte and an unsigned byte? ›

The key difference between unsigned and signed in our context is what happens when you assign an (unsigned) byte to an integer. If a signed byte is assigned, then the resulting integer is sign extended (i.e., 0xff becomes 0xffffffff).

What are signed and unsigned bits in Java? ›

Signed integers are stored in a computer using 2's complement. It consist both negative and positive values but in different formats like (-1 to -128) or (0 to +127) . An unsigned integer can hold a larger positive value, and no negative value like (0 to 255) . Unlike C++ there is no unsigned integer in Java.

Is 16-bit audio signed or unsigned? ›

16-bit audio is, by convention, usually signed. Think about what PCM audio is: each measure is how far along its axis the speaker should physically rest at that moment in time.

What signed bit means? ›

In computer science, the sign bit is a bit in a signed number representation that indicates the sign of a number. Although only signed numeric data types have a sign bit, it is invariably located in the most significant bit position, so the term may be used interchangeably with "most significant bit" in some contexts.

References

Top Articles
Latest Posts
Article information

Author: Frankie Dare

Last Updated:

Views: 6288

Rating: 4.2 / 5 (73 voted)

Reviews: 88% of readers found this page helpful

Author information

Name: Frankie Dare

Birthday: 2000-01-27

Address: Suite 313 45115 Caridad Freeway, Port Barabaraville, MS 66713

Phone: +3769542039359

Job: Sales Manager

Hobby: Baton twirling, Stand-up comedy, Leather crafting, Rugby, tabletop games, Jigsaw puzzles, Air sports

Introduction: My name is Frankie Dare, I am a funny, beautiful, proud, fair, pleasant, cheerful, enthusiastic person who loves writing and wants to share my knowledge and understanding with you.