Doubts about flaot, double, unsigned and signed

C, C++, Visual C++, C++.Net Topics
Post Reply
SukhdeepMankoo
Lieutenant
Lieutenant
Posts: 92
Joined: Tue Oct 27, 2009 7:50 pm

Doubts about flaot, double, unsigned and signed

Post by SukhdeepMankoo » Mon May 10, 2010 1:51 pm

Hi,
I am not able to understand the difference of
1) what is the difference of unsigned and signed.
As both can have same amount of memory eg. unsigned int and signed int have 16 bits(depending upon compiler). then what will be the benefit of signed.

2) what is the difference between float and double variables?
I have heard that float is a single precision and double is two precision, what is it?
I am completely dilemma about these things.
Kindly help me to know.
Thanks and regards
User avatar
Neo
Site Admin
Site Admin
Posts: 2642
Joined: Wed Jul 15, 2009 2:07 am
Location: Colombo

Re: Doubts about flaot, double, unsigned and signed

Post by Neo » Mon May 10, 2010 7:18 pm

1) what is the difference of unsigned and signed.
As both can have same amount of memory eg. unsigned int and signed int have 16 bits(depending upon compiler). then what will be the benefit of signed.
In simple terms, the difference is, singed data types can represent negative numbers where as the unsigned ones can only represent positive.
If I explain you a bit more, signed data type reserve the msb (most significant bit) to represent whether the number is positive or negative. If sign bit = 1, then number is negative and it is is zero, number is positive.

Read this post to have a simple explanation on this. https://robot.lk/viewtopic.php?f=47&t=1621
2) what is the difference between float and double variables?
I have heard that float is a single precision and double is two precision, what is it?
In computing, floating point describes a system for representing numbers that would be too large or too small to be represented as integers. Numbers are in general represented approximately to a fixed number of significant digits and scaled using an exponent. The base for the scaling is normally 2, 10 or 16. The typical number that can be represented exactly is of the form:

significant digits × baseexponent

The IEEE has standardized the computer representation for binary floating-point numbers in IEEE 754. There are two widely used formats descried in this standard as follows.
  1. Single precision - This is a binary format that occupies 32 bits (4 bytes) and its significand has a precision of 24 bits (about 7 decimal digits).
  2. Double precision - This is a binary format that occupies 64 bits (8 bytes) and its significand has a precision of 53 bits (about 16 decimal digits).
TypeSignExponentSignificandTotal bitsExponent biasBits precision
Single18233212724
Double1115264102353
If you want to learn more, you can refer following tutorials.
Introduction to IEEE 754 floating point representation
Floating-point Operations Tutorial
SukhdeepMankoo
Lieutenant
Lieutenant
Posts: 92
Joined: Tue Oct 27, 2009 7:50 pm

Re: Doubts about flaot, double, unsigned and signed

Post by SukhdeepMankoo » Thu May 13, 2010 5:09 pm

But still i have doubt.
Suppose in source file i have declared two variable.
char temp;
unsigned char utemp;

although, i can assign value from 0 to 255 to both variables and these variables store the value i will place,
e.g. temp=200;
utemp=200;

although, these two variables have 8-bit of memory for storage. then why need to declare char as both r same.
second thing, even we can achieve the same result for unsigned char as for char.
e.g. temp=-1;
utemp=-1;
both these variables have same value stored.
I am completely dilemmatic about it.
Can u elaborate it.
Thanks Neo.
User avatar
Neo
Site Admin
Site Admin
Posts: 2642
Joined: Wed Jul 15, 2009 2:07 am
Location: Colombo

Re: Doubts about flaot, double, unsigned and signed

Post by Neo » Thu May 13, 2010 5:46 pm

But still i have doubt.
Suppose in source file i have declared two variable.
char temp;
unsigned char utemp;

although, i can assign value from 0 to 255 to both variables and these variables store the value i will place,
e.g. temp=200;
utemp=200;
If you do the above, the values on both variables will become as follows.

temp = -56
utemp = 200

the reason is, the range of char (signed char) is from -128 to +127. When you put 200 (1100 1000) to it, it takes it as a negative value since the msb is 1.

unsigned equivalent of 'temp' can be taken by adding 256. (-56 + 256 = 200)
In other words 200 (1100 1000) in signed representation is -56.
Is it clear now?
although, these two variables have 8-bit of memory for storage. then why need to declare char as both r same.
second thing, even we can achieve the same result for unsigned char as for char.
e.g. temp=-1;
utemp=-1;
both these variables have same value stored.
If you do the above, the values on both variables will become as follows.

temp = -1
utemp = 255

Similarly, -1 + 256 = 255.

You can try putting 255 to both temp and utemp. temp will become -1 and utemp will become 255.
SukhdeepMankoo
Lieutenant
Lieutenant
Posts: 92
Joined: Tue Oct 27, 2009 7:50 pm

Re: Doubts about flaot, double, unsigned and signed

Post by SukhdeepMankoo » Fri May 14, 2010 12:28 pm

Thanks,
Post Reply

Return to “C/C++ Programming”