ASCII & 2's or 1's COMPLIMENT

Data structures & algorithms topics
Post Reply
User avatar
Face
Major
Major
Posts: 727
Joined: Thu Feb 18, 2010 5:06 pm
Location: SRI LANKA.KANDY.

ASCII & 2's or 1's COMPLIMENT

Post by Face » Tue Apr 06, 2010 4:55 pm

I heard that in computer I know it use Binary numbers for work.

In calculator application it use 2's complement to work.In note pad it use normally ASCII.
can any one tell me about the deference between these 2 & why It use these 2.?
why don't they use 2's complement in Note pad?

* in characters it contains numbers also but in ACCESS there are are options call numeric type & character type.?what is the deference between that..I ask this this because I feel this question have some relations with the questions above..

may be my idea is not clear for you..If it is not clear please ask.I'll try in again to explain my question in other way...!!
User avatar
Neo
Site Admin
Site Admin
Posts: 2642
Joined: Wed Jul 15, 2009 2:07 am
Location: Colombo

Re: ASCII & 2's or 1's COMPLIMENT

Post by Neo » Wed Apr 07, 2010 2:02 am

I'm clear on your question ;) However, there is a little mix (again :) ).

Basically, for numerical calculation both negative (-) and positive (+) values are involved.
For example: calculate 10 - 20 = -20

To deal with negative numbers, computers use a signed bit which is the most significant bit (msb) in a little-endian system. A number with a sign bit is called Signed number. If you are so sure, that you don't need negative numbers, then you can disregard the sign bit and use the full range of bits.

In C, we have two data types for 1 byte data as below.
signed char val; (or simply char val;) can store numbers from -128 to +127
unsigned char val; can store numbers from 0 to 255

Similarly, we have same for 16-bit and 32-bit data types.

To calculate signed numbers, the method we use in computers is called 2's compliant system.

Definition
The two's complement of a binary number is defined as the value obtained by subtracting the number from a large power of two (specifically, from 2N for an N-bit two's complement). The two's complement of the number then behaves like the negative of the original number in most arithmetic, and it can coexist with positive numbers in a natural way.

A 2's-complement system or 2's-complement arithmetic is a system in which negative numbers are represented by the 2's complement of the absolute value; this system is the most common method of representing signed integers on computers. In such a system, a number is negated (converted from positive to negative or vice versa) by computing its two's complement. An N-bit 2's-complement numeral system can represent every integer in the range ?2^(N?1) to +2^(N?1)?1.

For example, if N=8 (that is 8-bit or 1 byte), the range is -2^7 to +2^2-1. That is -128 to +127.

Now let see how we are going to store -96 in a signed char type (1-byte).

?95 + 256
= ?95 + 255 + 1
= 255 ? 95 + 1
= 160 + 1
= 161

Now if you store 161 to a byte, in singed representation, it is -96. In unsigned representation, it is 161.

Code: Select all

  1111 1111                       255
- 0101 1111                     ?  95
===========                     =====
  1010 0000  (ones' complement)   160
+         1                     +   1
===========                     =====
  1010 0001  (two's complement)   161

Okay. Now I think you are clear on that. The next is about having numbers, characters, etc...

Computers use to store numbers using the same ASCII character set (0 - 255). Say if you have +#?< characters in memory, this represent a number.

Character 1 in ASCII is 49. So if we store number 49 to a memory location and read it as a character, what you read is 1. If we store 43 to memory and read it back as a character, you will see "+".
User avatar
Face
Major
Major
Posts: 727
Joined: Thu Feb 18, 2010 5:06 pm
Location: SRI LANKA.KANDY.

Re: ASCII & 2's or 1's COMPLIMENT

Post by Face » Wed Apr 07, 2010 9:22 pm

yep..BRo..
Now I clear with this.
Thans..!
User avatar
Face
Major
Major
Posts: 727
Joined: Thu Feb 18, 2010 5:06 pm
Location: SRI LANKA.KANDY.

Re: ASCII & 2's or 1's COMPLIMENT

Post by Face » Fri Apr 09, 2010 9:06 pm

wait wait friend I have a question.....

in your example you explain about - 95 & how it represent in 2's complement.
it represent in 8 bits.in computer -95 store such like that....but how can store number > -127 in that way.....??
User avatar
Neo
Site Admin
Site Admin
Posts: 2642
Joined: Wed Jul 15, 2009 2:07 am
Location: Colombo

Re: ASCII & 2's or 1's COMPLIMENT

Post by Neo » Sat Apr 10, 2010 4:16 am

but how can store number > -127 in that way.....??
I think you refer, how to show a number < -127 (Not > - 127). You know I'm also a mathematician so I care about even small things to make the perfect answer. :D

Another point is, signed byte can represent numbers from -128 to +127.
So logically for your question, I would say you can still use it since -128 < -127 :) .

See one number and a symbol can make a big difference. Okay, lets get on with it....

As said, -128 to +127 is the range for singed byte. (i.e.: If we use a single byte for negative values).

If we use two bytes (this is usually called short integer) then the range is –32768 to 32767.

See C++ integer ranges below.
TypeMinimum and MaximumNo. of Bytes
unsigned char0 to 2551
signed char-128 to +1271
unsigned short0 to 655352
signed short–32768 to 327672
unsigned long/unsigned int0 to 42949672954
signed long/signed int–2147483648 to 21474836474
You can see a complete list at Integer (computer science).

So if you need to store a large number, you will need to change the data type you use to define the variable. Clear ?
User avatar
Face
Major
Major
Posts: 727
Joined: Thu Feb 18, 2010 5:06 pm
Location: SRI LANKA.KANDY.

Re: ASCII & 2's or 1's COMPLIMENT

Post by Face » Sat Apr 10, 2010 9:17 am

YEAH..got it...thank you NEO bro..
Post Reply

Return to “Data Structures & Algorithms”