Signed vs Unsigned

Han sang yeob·2021년 2월 8일
0

Basic of C language

목록 보기
4/9
  1. Signed number
  • That can be positive or negative
  1. Unsigned number
  • The numbers that can only be positive

    The computer represents all the data using binary. It is impossible for us to identify the data types by looking at the binary code because any given binary code could mean anything. Maybe a number, text, part of the movie etc..

ex) 111 -> 7

If I ask the computer to store 111 inside of the computer, what is the minimum size that you require? The answer is 3 bits. Each bit in my computer is effectively a single 0 or 1. And 3 of those bits will be big enough to store any value through 0 to 7.
But what happens if we want to store the number 8??
Well, we can do that with only 3 bits. We need at least 4 bits because 8 is represented by using 4 binary digits.

eight : 1000

since the eight is represented with 4 binary digits, we need at least 4 bits in order to store the number in our computer. If we need to store the number 16 or greater, we need at least 5 bits. So, here we learned very important principles.

  1. The number of bits determines the maximum size of any number.

  2. Adding just one extra bit to any binary sequence doubles its capacity.

    0000 (0) 1000 (8)
    0001 (1) 1001 (9)
    0010 (2) 1010 (10)
    0011 (3) 1011 (11)
    0100 (4) 1100 (12)
    0101 (5) 1101 (13)
    0110 (6) 1110 (14)
    0111 (7) 1111 (15)

Between 0 and 15, we have stored total of 16 different values.You any noticed, we simply repeated 0 - 7 twice only adding 1 in front of it. This is because 1 on the right front, add 8. The far-left digits 0,1 is a flag. We can say that if the far left numbers are set to 0, it means positive, and if it is set to 1, it , means negative. Whenever we incode a number in binary, that it can be either a positive or negative number which is known as a signed number.
This method is called signed magnitude.It simply means that the furthest digit over to the left is a flag indicating whether or not the number is positive or negative. So for example, 0011 is positive 3, and 1011 is negative 3. Whenever we define a bit as a flag for stating if a number is positive or negative, that bit is called the sign bit. In this case, a sign bit of zero means a positive number, and a sign bit of one means a negative number.

Now here we should notice something important. When we are using 4 bits, if this were an unsigned number, we can count all the values from 0 to 15.
However, when we make the eights place into a flag for positive or negative numbers, we can only count half as much as before but we can do so in two different directions. so we can count from 0 to 7 and then we can count from 8 - 15 or from 0 to negative 7.

  1. You can specify a number a being negative or positive with a "sign bit".

  2. When you have a sign bit, you can only count half as far but you can do so in two directions,
    positive and negative.

  3. The same exact binary can encode a signed number or an unsigned number.
    For example, the binary sequence 1010 could mean either "ten" or "negative two" in our example.

profile
Learning IT in Vietnam

0개의 댓글