Sona said:
Hi,
Could someone please explain what sign-extension means?
Sign extension is usually a low-level (i.e. assembly) processor term.
In most applications using the C language, there is no concern about
sign extension as that is accounted for in the definition of the
language.
Basically, sign extension is extending the sign of a number (positive
or negative) from a single unit integer to a multi-unit integer.
For example, if you have an integer representing 0x55 and wish to
use two integers (to extend the range), you would set up the second
integer to be zero, which is the sign for a positive number (not
true for all platforms). Negativity is a bit different.
Let us assume for example, that in a given system, -1 is represented
by all bits set to one. When using two integers, the combination
must represent -1. So, the second integer is set to all ones to
extend the sign of the first integer.
Search for these programming concepts:
Multiple Precision Arithmetic
One's Compliment
Two's Compliment
If I have a hex number 0x55, how does this get sign-extended?
See above.
Can a sign-extended counterpart be equal to -91?
I believe you are confusing sign-extension with signed representation
of a number.
In Twos Compliment notation, I am get 0xA5 as -91 (8-bit unit).
Sign extending to 16-bits results in 0xFFA5, to 32 bits: 0xFFFFFFA5.
In a program I'm expecting 0x55 in return
from a function whereas I am getting -91 every time.. does this mean
anything? Thanks
Sona
I have no idea. There may be an infinite number of relationships
between -91 and 0x55; Two's Complement negativity isn't one of them.
Have you tried single stepping through the function with a debugger?
Or even using printf statements within the function?
--
Thomas Matthews
C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq:
http://www.parashift.com/c++-faq-lite
C Faq:
http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book