Click to See Complete Forum and Search --> : minus number
Alkingg
April 2nd, 2005, 12:42 PM
hi all,
im trying to write a negative number on its binary form on a file and then i need to reconstruct these number again .........when i convert -5 to its binary equivelant i get 11111011 to convert this number to its decimal equivalent u will get 251 so;
i need a way to reconstruct the negative number?????
cilu
April 2nd, 2005, 02:38 PM
First, you should post the code you're using so we could see what you're doing wrong.
The signed integer -5 and unsigned integer 251 have the same bit representation. Except that the bits have different meaning, because the unsigned version does not have a sign bit.
HighCommander4
April 2nd, 2005, 05:44 PM
When you "reconstruct" the number, reconstruct it to a signed char (or any signed integer type) as opposed to an unsigned integer type.
matthias_k
April 3rd, 2005, 04:40 AM
hi all,
im trying to write a negative number on its binary form on a file and then i need to reconstruct these number again .........when i convert -5 to its binary equivelant i get 11111011 to convert this number to its decimal equivalent u will get 251 so;
i need a way to reconstruct the negative number?????
And how do you convert it? You have to use the 2s-complement, that is, flip all bits and add one.
RoboTact
April 3rd, 2005, 05:17 PM
char val=-5;
cout<< "signed value: "<<(int)val<<endl;
unsigned char uval=val;
cout<<"unsigned value: "<<(int)uval<<endl;Output:signed value: -5
unsigned value: 251
dude_1967
April 4th, 2005, 09:33 AM
Indeed Alkingg,
Explicitly sumarizing the previous posts, most compilers for modern CPU architectures store the negative bit of a signed integer type in the highest bit position. This means that negative integers, at the bit level, look like large positive integers. If you really want to examine the bit stream, the you can shift the bits out one at a time.
Chris.
:)
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.