Signed and Unsigned Bit Integers: What Does It Mean and What's the Difference? (2023)

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

Aidi Rivera

Published in

#Beginner #codeneuling #Computer's science #today I learned

In my previous blogs, I've given an overview of what it means to work with a binary or 8-bit, 16-bit, 32-bit, etc. number. and how would you solve an algorithm problem involving a specific integer -Bit required size without the computer science background to understand the whole thing. This post specifically addresses what exactly it means to have a signed or unsigned binary number. It won't change much how integers are constrained when solving algorithm sets, but it will drastically change the range you can work with. So I will solve the same problem.until nowbut receptive to help solve asignedbinary integer instead of a non-binary one.

What does that mean?

The main difference between a signed and unsigned binary number is that the leftmost bit is used to indicate whether the number has a one or not.minus. The rest of the bits are used to indicate the normal value.

This first bit, the sign bit, is used to indicate whether it is positive (with 0) or negative (with 1). If you want to get technical, a sign bit of 0 indicates that the number is anot negative, which means that it can be equal to a decimal zero or a positive number.

(-/+)26252423222120
01001101

010011012= +7710

Note: I'm using X2Notation for binary integers and the X10Notation for decimals.

More importantly, the first bit used to denote the sign means that we havea little lessdesignate value. So if we have an 8-bit signed integer, the first bit tells us whether it is negative or not, and the other seven bits tell us what the real number is. Because of this, we technically work with a more limited range of representable numbers; 7 bits cannot store numbers as large as 8 bits.

What is the difference?

unsigned binary numbers

Forcheck binary numbers, the ones and zeros act like switches, metaphorically turning on powers of 2 and then adding them to produce the decimal value. Normally we would "mark" a bit value with a one.

Example 1a:
0101 unsigned2= 510

23222120
0101

Example 2a:
unsigned 10012= 910

23222120
1001

signed binary numbers

Example 1b:
signed01012= +510

(-/+)222120
0101

If a signed binary number is positive or negative, it is "marked" with 0 or 1 in the first leftmost bit, the sign bit. The number above does not change anything. It is more explicitly a positive number.

Example 2b:
signed10012= -710

(-/+)222120
1001

But the above binary number changes completely. And now we present a negative!

negative binary integers

If a binary integer is negative, thezerosnow acts as one "marker" instead of two. You would then calculate the negative binary number the same way you would calculate a positive or unsigned integer, but use zeroes as placeholders to turn on bit values ​​instead of ones, and then add the negative sign inMovieyour calculation

(-/+)222120
1001

signed10012= -710

The transition from an unsigned binary to a signed binary integer changes its final value in several ways. The first is the most obvious value change when the first bit is used to indicate a sign instead of a value. You can see from examples 2a and 2b above that if you have a 1 in the first bit of your 4-bit integer, this means that you are missing a value of 2.3which would have been added to its final value with an unsigned bit, but is now used to represent a negative. A larger integer bit can be an extremely large value that you can no longer represent.

Another thing that is not immediately obvious is that you are calculating the value of a negative binary integer that starts at 1, not 0. Since decimal zero is not contained in a bit negative signed integer, we do not start counting from zero as we would when it is a positive signed integer bit.

To explain this peculiarity, let's compare integers with positive and negative signs. If we were dealing with a 4-bit integer and we had four zero-value bits, the number would equal 0. This is the smallest value we can have. Because anot negativeThe signed bit means that we can have a positive integer or a 0.

0000= 010

A 4-bit negative 4-bit integer with values ​​of one (which are now the "off switch"), the number would not equal 0 but -1. Which makes sense since it's the largest decimal we can represent while still having a negative value.

1111= -110

But that means that when we add our values ​​to get our final decimal, we start our count at 1, not 0.

2X(-/+)222120Total
bit value0110
2Xbit value x =+420= 610

So even if you were to seamlessly change the "changes" of the positive signed binary number above to its negative counterpart, it would not seamlessly change to its negative decimal equivalent as you might expect:

2X(-/+)222120Total
bit value1001
2Xbit value x =-42= -710

Why??

Because we add from a value of 1! And we add the values ​​represented in our bits before adding a minus signa lot ofend of our account.

Here's a visual comparison of the decimal and binary equivalents, showing how an integer of signed 0 bits is decimal 010or greater, while a 1-bit signed integer is -1 decimal10or smaller.

Signed and Unsigned Bit Integers: What Does It Mean and What's the Difference? (3)

Alternative:

Another way to calculate the negative is to still use the ones as "placeholders" and use the sign bit as a placeholder for the value in its corresponding power of two in oneNegativeWorth. This also illustrates another way of understanding what happens in negative binary representations.

2X(-/+)23222120Total
bit value1001
2Xbit value x =-8001= -710

This way of calculating the decimal value may be a bit easier when you're working with smaller decimals, but it becomes a bit more difficult to do mental math when you're working with larger decimals:

2X(-/+)2726252423222120Total
bit value11110010
2Xbit value x =-1286432sixteen0021= -1410

Fortunately, there aren't many situations where you need to interpret between the two without a calculator!

Finding the maximum and minimum with signed binary numbers

The range of positive decimal numbers that can be stored in an integer bit of any size is reduced by using the first bit to indicate the sign. This means that in the case of a 32-bit signed integer, we are actually dealing with 31 bits instead of 32, and the last bit could have stored aexponentiallygreater integer. Indeed it is completeHalfThe range of positive integers we can work with, compared to a 32-bit unsigned integer. That extra bit would have doubled our maximum possible integer, and without it we would lose the ability to store that many positive integers.

On the other hand, we've gained the ability to store a range of negative integers that we couldn't before with an unsigned integer bit. In the end, the size of the strip we are working with stays the same, but the strip is moved so that it can store the most positives.minegative numbers.

Let's look at an unsigned versus a signed 4-bit integer. Our reach may move, but theHeightof the integers that can be stored does not actually change.

Signed and Unsigned Bit Integers: What Does It Mean and What's the Difference? (4)

Due to this one-bit loss, our maximum is calculated using2bits - 1- 1, or when working with 32-bit integers231- 1.
I explained why we need to figure that out.last time, which we still need to do since we are including zero in the range and no subtraction would require an extra bit to store that number.

Our minimum on the interval is the inverse,-2bits - 1, or if you are working with 32-bit integers,-231. We do not subtract one for our minimum range since zero is not included and we start counting from -1. This gives us the extra negative number in our range to represent.

If you see the range above, you can imagine why there is no subtraction for the lower range, while there is for the upper range. The zero is included in the green area but not in the red area of ​​the signed bits. We start at -1 and can have as many numbers represented as non-negative.

********
-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 positives.minegative integers. But still only 8 integers.

The problem

Going back to the problem solved in the last post, this time the solution is to create a restricted scope for asignedall.

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

We know that this is a 32-bit integer with 32 0's and 1's, the first of which denotes the sign.
If we were dealing with 31 bits that could represent the value of the number, the largest positive binary integer we could have would be 31 units past the first null sign bit, giving us a positive sign.

That means the largest decimal we could handle would be231- 1, o2.147.483.647

The largest (and by largest I mean smallest?) negative binary integer would be 31 zeros with the sign bit one, which tells us that it is negative.

That means the smallest decimal we could handle would be-231o-2.147.483.648

The solution

until reverse integer = (X) => { leaving invested = ""; strx = X.sequence(); Pro(leaving number Von strx){ invested = number + invested } invested = parseInt(invested, 10); what if(X<0){ invested = invested * -1; } //setting the range! what if (invested > mathematics.to defeat(2, 31) - 1 || invested < mathematics.to defeat(-2, 31)) Returns 0; Returns invested;};

The problem is essentially making sure that we don't return a number that can't be stored as a 32-bit signed integer. Here we will skip the solution to this problem and focus on the range while I went through the solution.until now.

The line just beforeReturnschecks if final integer is contained ininvestedis within reach. Yeahinvestedis better than231- 1the less than-231, returns 0.

If it were a 32-bit unsigned integer, it would have a range of 0 to 232-1 or 4,294,967,295. This top rank is twice the rank of 231. You can think of the missing "half" of the range that would have stored those positive numbers as being used to store your negative numbers. Same size range, just different start and end points in that range.

Fin!

That ends my series on binary numbers for the average computer science graduate!

This was a really fun (and frustrating) learning curve. I hope there are gaps in my summary, as there is a lot to report without needlessly delving.

I want this to be a good starting point for those who want to learn the basics. So if there's something I don't clearly understand (or I assume you know something you don't), let me know!

Happy coding!

Resources:

Reverse integer LeetCode problem
signed binary numbers
Decimal to Binary Converter
Signed figures - Watson
Rundungsalgorithmen 101 Redux - EETimes
Bits, Bytes, and Integers - Carnegie Mellon

Top Articles
Latest Posts
Article information

Author: Gregorio Kreiger

Last Updated: 03/30/2023

Views: 5803

Rating: 4.7 / 5 (57 voted)

Reviews: 80% of readers found this page helpful

Author information

Name: Gregorio Kreiger

Birthday: 1994-12-18

Address: 89212 Tracey Ramp, Sunside, MT 08453-0951

Phone: +9014805370218

Job: Customer Designer

Hobby: Mountain biking, Orienteering, Hiking, Sewing, Backpacking, Mushroom hunting, Backpacking

Introduction: My name is Gregorio Kreiger, I am a tender, brainy, enthusiastic, combative, agreeable, gentle, gentle person who loves writing and wants to share my knowledge and understanding with you.