unsigned and signed numbers

Thread Starter

Parth786

Joined Jun 19, 2017
642
I have studied unsigned and signed in c programming but still I am confuse. I just want to make sure that my understanding is right or wrong

Let take a simple example. Suppose we have a 8-bit binary representation of both unsigned and signed numbers

upload_2017-10-26_18-49-31.png

We write unsigned int and signed int in program

unsigned int = (0 to 255) // can we store only 0 to 255 decimal numbers?

unsigned int n1 = 5

unsigned int n2 = 5

unsigned int n3 = 5

Does it mean we can store decimal 5 number from 0 to 255 time ?

Same for signed integer

signed int n1 = 5

signed int n2 = 5

signed int n3 = 5

signed int n1 = -5

signed int n2 = -5

signed int n3 = -5

Does it mean we can store decimal 5 number from 0 to 127 times and – 5 number from -128 to -1 times ?

Note : I am assuming size of register is 1 byte. It may be 2 bytes or 4 bytes depend on hardware
 

MrChips

Joined Oct 2, 2009
30,824
I have studied unsigned and signed in c programming but still I am confuse. I just want to make sure that my understanding is right or wrong

Does it mean we can store decimal 5 number from 0 to 255 time ?

Does it mean we can store decimal 5 number from 0 to 127 times and – 5 number from -128 to -1 times ?

Note : I am assuming size of register is 1 byte. It may be 2 bytes or 4 bytes depend on hardware
Your question is not clear.

What do you mean by "0 to 255 time" ?

This is not clear:
"Does it mean we can store decimal 5 number from 0 to 127 times and – 5 number from -128 to -1 times ?"
 

Thread Starter

Parth786

Joined Jun 19, 2017
642
Your question is not clear.
"
unsigned range 0 to 255
signed range -127 to -1 and 0 to 127

let suppose we have variable N1, N2, N3 .... up to 255

Declaration and initialization of variable
unsigned int N1 = 5;
unsigned int N2 = 2;
unsigned int N3 = 15;
--- --- ---- ---
unsigned int N253 = 50;
unsigned int N254 = 20;
unsigned int N255 = 15;

What the purpose of unsigned int ? I think it can only store 0 to 255 variable and the value of data should be positive number.

1. limit is 0 to 255. it will not store 256 variable compiler will give error
2. if I write unsigned int means variable store only positive number like 2 , 256 , 456, 4569,

same for signed int

signed int N1 = 5;
signed int N2 = 2;
signed int N3 = 15;
--- --- ---- ---
signed int N253 = 50;
signed int N254 = 20;
signed int N255 = 15;

I think it can only store (0 to 127 and -127 to -1 ) variable and the value of data should be positive number.

1. limit is 0 to 127. it will store 0 to 127 variable but decimal number will positive
2. limit is -1 to - 127 it will store 1 to 127 variable but decimal number will negative

I think unsigned int will store only positive number but the limit of variable will be 0 to 255 and signed will store both positive and negative number and limit of variable to store positive number is 0 to 127 and limit of variable to store negative number is -1 to -127 and

Now Do you understand my question? Does it make any sense ?
 
Last edited:

Thread Starter

Parth786

Joined Jun 19, 2017
642
It is as clear as mud.
Why do you need 255 variables, N1 to N255?
That is just example for deep understanding.

Let suppose we have 8 bit memory whose address range is 0 to 127 and - 1 to -127

signed int range is 0 to 127 and - 1 to -127

if I want to store decimal number 8 then it will store address between o to 127
signed int = 8;

if I want to store decimal number -8 then it will store address between -1 to -127
signed int = -8;

Am I correct ?
 

joeyd999

Joined Jun 6, 2011
5,287
That is just example for deep understanding.

Let suppose we have 8 bit memory whose address range is 0 to 127 and - 1 to -127

signed int range is 0 to 127 and - 1 to -127

if I want to store decimal number 8 then it will store address between o to 127
signed int = 8;

if I want to store decimal number -8 then it will store address between -1 to -127
signed int = -8;

Am I correct ?
Why are you conflating addresses with values? They are separate and distinct concepts.
 

Thread Starter

Parth786

Joined Jun 19, 2017
642
Why are you conflating addresses with values? They are separate and distinct concepts.
I understand data and address are two different things. I am confuse on this statement signed int range is 0 to 127 and - 1 to -127. I don't understand why there is positive and negative range

I tried to explain with my example for signed or unsigned. if possible can you explain with your example
Why have 255 or 256 variables when a single variable will do?
I agreed Question is not, what it can do. question is, what is purpose of negative and positive range why does it have tow ranges 0 to 127 and - 1 to -127
 

joeyd999

Joined Jun 6, 2011
5,287
I don't understand why there is positive and negative range
Well, by definition, the integer data type holds values that have a sign.

The unsigned integer data type does not have a sign.

How would you define a negative value without a signed data type?

FYI, addresses typically don't use "signed" pointers. Addresses are positive in value.
 
Last edited:

Thread Starter

Parth786

Joined Jun 19, 2017
642
Well, by definition, the integer data type holds values that have a sign.

The unsigned integer data byte does not have a sign.

How would you define a negative value without a signed data type?

FYI, addresses typical don't use "signed" pointers. Addresses are positive in value.
Sorry but not clear for me. I have two option. When to select right one and why it is right ?
When will use unsigned int ?
C:
#include <stdio.h>

int main (void)
{
     unsigned int variable =  ; 
 
     printf (" variable value :   %d   \n", variable);
 
    return 0;
}
When will use signed int ?
C:
#include <stdio.h>

int main (void)
{
     signed int variable =  ; 
 
     printf (" variable value :   %d   \n", variable);
 
    return 0;
}
Note: Its not complete program. I have not assigned variable value
 

AlbertHall

Joined Jun 4, 2014
12,347
The number of bits the compiler uses to store an integer type varies between compilers but is commonly 16.
unsigned int can store values between 0 and 65535
signed int can store values between -32768 and +32767

char variables generally use 8 bits.
unsigned char can store values from 0 to 255
signed char can store values from -128 to +127

You would choose to use char or int and signed or unsigned based on what values you want to store in those variables.
If you want to use values more negative than -128 then the only option is to use signed int as it is the only type which can store that value.
If the values you want use will always be positive and might be up to 255 then you could use unsigned char. In this case you could also use either signed or unsigned int as both of these can store those values. However you probably wouldn't want to do that as it would use an extra byte and give you no benefit.
 

joeyd999

Joined Jun 6, 2011
5,287
The number of bits the compiler uses to store an integer type varies between compilers but is commonly 16.
unsigned int can store values between 0 and 65535
signed int can store values between -32768 and +32767

char variables generally use 8 bits.
unsigned char can store values from 0 to 255
signed char can store values from -128 to +127

You would choose to use char or int and signed or unsigned based on what values you want to store in those variables.
If you want to use values more negative than -128 then the only option is to use signed int as it is the only type which can store that value.
If the values you want use will always be positive and might be up to 255 then you could use unsigned char. In this case you could also use either signed or unsigned int as both of these can store those values. However you probably wouldn't want to do that as it would use an extra byte and give you no benefit.
Of course, everything thing you say is correct. But I fear you just confused him further.
 

Thread Starter

Parth786

Joined Jun 19, 2017
642
signed int can store values between -32768 and +32767
.
According to the statement. it will store only 32767 and it will not store 32768
C:
#include <stdio.h>

int main (void)
{
     signed int variable = 32768;   
   
      printf (" variable value : %d \n", variable);
   
      return 0;
}
Result variable value : 32768

What's wrong. why does it not follow this statement "signed int can store values between -32768 and +32767"
 

joeyd999

Joined Jun 6, 2011
5,287
According to the statement. it will store only 32767 and it will not store 32768
C:
#include <stdio.h>

int main (void)
{
     signed int variable = 32768;  
  
      printf (" variable value : %d \n", variable);
  
      return 0;
}
Result variable value : 32768

What's wrong. why does it not follow this statement "signed int can store values between -32768 and +32767"
Read the reference I posted above. The hardware you are using likely considers integers to be 32 or 64 bits, which can contain much larger values.

The integer datatype (in C) is hardware -- not compiler -- dependent.
 

Thread Starter

Parth786

Joined Jun 19, 2017
642
I think I got it concept

Unsigned integers
Code:
0000 0000 0000 0000     0
0000 0000 0000 0001     1
0000 0000 0000 0010     2
0000 0000 0000 0011     3


-----   ------    ---------      
1111 1111 1111 1111     65535
binary 1000 0000 0000 0000 0 = 65536 decimal

65536 require 17 bit but the size of register is 16 bit so it will not store 65536

Example in c
This is valid statement Unsigned int = 65535;
This is not valid statement Unsigned int = 65536;

Signed integers
Code:
0111 1111 1111 1111      -32767
0111 1111 1111 1110      -32766


------   ------   ------            
0111 1111 1111 1110        32766
0111 1111 1111 1111        32767
-32768 require 17 bit but the size of resister is 16 bit so it will not store -32768

Example in c

Signed int = -32767; This is valid statement
Signed int = -32768; This is not valid statement
Signed int = 32767; This is valid statement
Signed int = 32768; This is not valid statement

Now Does it make sense ?

Note : I am just assuming size of register is 16 bits
 

WBahn

Joined Mar 31, 2012
30,077
According to the statement. it will store only 32767 and it will not store 32768
C:
#include <stdio.h>

int main (void)
{
     signed int variable = 32768;  
  
      printf (" variable value : %d \n", variable);
  
      return 0;
}
Result variable value : 32768

What's wrong. why does it not follow this statement "signed int can store values between -32768 and +32767"
The first thing you need to do is determine how many bytes of storage your compiler actually uses for the data type you are interested in. You can do this with the following:

[CODE = C]
#include <stdio.h>

int main(void)
{
printf("char: %i bytes\n", sizeof(char));
printf("short: %i bytes\n", sizeof(short));
printf("int: %i bytes\n", sizeof(int));
printf("long: %i bytes\n", sizeof(long));
printf("long long: %i bytes\n", sizeof(long long));

return 0;
}
[/CODE]

The most common situation today is that an int is either 4 bytes (32 bits) or 8 bytes (64 bits). The C language standard only requires that it be a minimum of 16 bits, which was very common a couple decades ago. You don't have any direct control over how many bits each data type is, that is defined by the compiler you are using (which is usually done with thoughts toward the capabilities of the hardware being targeted).

If you have a C99 compliant compiler, then you can include the stdint.h header and use the fixed-width data types such as

int16_t and uint16_t for the signed and unsigned 16-bit data types. These are just typedef statements that resolve to whatever internal data type is 16 bits (which is usually a 'short').

So let's say that you use a 16-bit data type. With sixteen bits, you can represent 2^16 distinct 16-bit patterns, or 65,536 of them. If we use a signed data type, then all 65,536 values are non-negative and they represent the values from 0 up through and including 65,535. But we often need to be able to work with negative as well as positive values. When we do, we have to choose to use some of those patterns to represent negative numbers. There are several ways to do this. The most common is two's complement, in which the value represented by a N-bit pattern is defined as follows:

If, interpreted as an unsigned N-bit pattern, the value would be LESS than 2^(N-1), the value actually represented is the same as if it were an unsigned value. In other words, if the msb (the 'sign' bit) is a 0, the value represented is the same whether the representation used is signed or unsigned.

Otherwise, if the unsigned N-bit pattern would be greater than or equal to 2^(N-1) (meaning the sign bit is a 1), then the value represented is the value of the unsigned N-bit pattern less 2^N.

For a 16-bit representation, 2^(N-1) is 32,768. So the nonnegative values that can be represented are from 0 to 32,767. At that point, if we add 1, we get 32,768, which has the sign bit set, so we need to subtract 65,656 to determine the actual value represented and we would learn that it is -32,768. If we keep adding one, eventually we get to a bit pattern that is all 1's, which would be 65,535 as an unsigned integer. Since the sign bit is still set, we need to subtract 65,536 to get the actual value represented and we learn that it is -1. Adding one again takes us to a pattern of all 0's, which represents 0.
 
Top