answersLogoWhite

0


Best Answer

In C Programming, the signed and unsignedmodifiers only apply to integer data types: char, short int, int, long int and long long int. Using these modifiers affects the range of values that each of these types can physically represent, however those ranges are implementation-defined.

Although we typically regard a byte as being 8 bits in length, this is not the case at all. In programming, a byte is simply the smallest unit of addressable storage on the system, but the length of a byte is actually determined by the machine architecture of that system.

In C programming, all data types are measured in chars, thus sizeof (char) is always 1 (byte). However, to determine the number of bits per char we need to examine the CHAR_BITS macro defined in . In most cases this macro is defined with a value of 8 (bits), however we can never assume that is always the case; some systems use a 9-bit byte, others use a 16-bit byte. The only thing the standard guarantees is that CHAR_BITS will be no less than 8. Although 7-bit and 6-bit systems do exist, they are non-standard and therefore require non-standard language implementations.

Generally, we don't really need to know the bit-length of an individual char, we simply need to know what range of values the integer types can physically represent (with respect to the current implementation). Again, we look to the macros defined in the implementation's header:

SCHAR_MIN : Minimum value signed char

SCHAR_MAX : Maximum value signed char

UCHAR_MAX : Maximum value unsigned char

CHAR_MIN : Minimum value char

CHAR_MAX : Maximum value char

SHRT_MIN : Minimum value short int

SHRT_MAX : Maximum value short int

USHRT_MAX : Maximum value unsigned short int

INT_MIN : Minimum value int

INT_MAX : Maximum value int

UINT_MAX : Maximum value unsigned int

LONG_MIN : Minimum value long int

LONG_MAX : Maximum value long int

ULONG_MAX : Maximum value unsigned long int

LLONG_MIN : Minimum value long long int

LLONG_MAX : Maximum value long long int

ULLONG_MAX : Maximum value unsigned long long int

Note that there are no macros defining the minimum value of unsigned data types because the minimum value for any unsigned data type is always 0. Also, a "plain" int is always signed, hence there is no SINT_MIN or SINT_MAX macro.

The char, signed char and unsigned char are three distinct types. The standard does not specify whether a "plain" char should be signed or unsigned, but its range must match that of either the signed char or unsigned char data types. To determine whether a plain char is signed or not, we can use the following:

bool signed_char = ((int) CHAR_MAX == (int) SCHAR_MAX) ? true : false;

If signed_char is true, a char is equivalent to a signed char, otherwise it is equivalent to an unsigned char.

Negative integer values are either represented using ones-complement or twos-complement notation. Again, this is implementation-defined although most modern systems use twos-complement. To determine which notation is in use, we simply look to the minimum value of a signed char. Assuming an 8-bit char, a ones-complement system defines SCHAR_MIN as being -127 while a twos-complement system uses -128.

In ones-complement notation, to flip the sign we simply invert all the bits, thus 01010101 becomes 10101010 (the ones-complement of 01010101). The high-order bit denotes the sign (0 for positive, 1 for negative). However, this then means that we have two distinct representations for the value zero: 00000000 (+0) and 11111111 (-0) but zero is neither positive nor negative. To eliminate this inconsistency, twos-complement adds one to the ones-complement, thus 11111111 + 1 = 00000000 (the overflowing bit is simply ignored). By eliminating the redundant representation for -0, the negative range of values increases by 1.

One of the implications of using signed and unsigned data types is that we must be careful when performing mixed-mode arithmetic as this can result in "narrowing" or loss of information. For instance:

void f (signed char s, unsigned char u) {

u = (unsigned) s; // ouch!

}

Here we used an explicit cast, however if s is negative we will lose information because an unsigned type cannot represent a negative value. Conversely, if s is positive, we don't lose information because the upper range of u exceeds that of s. So before converting between signed and unsigned representations, it is worth ensuring the value is within the range of valid values. When converting from unsigned to signed, it's usually a good idea to use a larger signed data type.

User Avatar

Wiki User

6y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What are the implications of using signed vs unsigned bytes in a computer system?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

What number system is used to display a memory address?

it is decimal unsigned number system...


How can you access the unsigned drivers options?

Click on System Properties and then the Hardware Tab


What version of Opera Mini does the LG KS360 need?

Since there is no signed version available, you will have to use the generic version with MIDP 2. However, since it is unsigned, Opera Mini will not be able to access your system, so you can't download anything from it.


What are the implications of using offsets by an operating system?

your mom


What are the ethical implications of an individual reward system?

The ethical implications of the individual reward system is that it is open to corruption and other malpractices. This usually leads to collapse of the public systems.


What is the full form of ULS?

ULS is an acronym for Universal Licencing System.


What is unsigned data types in turbo c?

The same as an unsigned type in any other implementation of C. An unsigned type is an integer that is guaranteed positive. Normally, the most-significant bit of an integer denotes the sign (positive or negative). Unsigned types use this bit to denote value, effectively doubling the range of positive values over that of the signed equivalent. For instance, a signed char has a guaranteed range of -127 to +127 while an unsigned char has a guaranteed range of 0 to 255. Note that a signed char typically has a valid range of -128 to +127, however this is only true on systems that utilise twos-complement notation. Those that use the older ones-complement notation have two representations for the value zero (one positive, one negative). Ones-complement simply inverts all the bits of a value to switch the sign of a value, whereas twos-complement adds the value 1 after inverting all the bits. The value zero is denoted as 00000000 in binary. Inverting the bits creates 11111111, which is minus zero on a ones-complement system and -1 on a twos-complement system. -1 + 1 is 0, hence we add 1 on a twos-complement system.


Study of the implications of the immune system in psychiatry?

Psychoimmunology


How does the 68HC11 know whether we are using the 2's complement system or not?

It doesn't! Only the programmer does. The C (carry) flag indicates an overflow/underflow for unsigned numbers and the V (overflow) flag indicates an overflow/underflow for signed numbers. When the processor does a computation both flags are set appropriately, but you're only interested in the one that corresponds to whatever number system you chose to use.This is why there are different branch instructions for signed and unsigned numbers. For example, BHI is branch if higherand BGT is branch if greater than. These sound like the same thing, but BGT is used for comparisons of signed numbers and BHI is for unsigned. These branches don't know what number system you're using -- they just know what flags to look at. The combination of flags set by the comparison will cause BGT to branch if the numbers being compared are signed (and the second number is greater than the first) because it looks at the Z, N, and V flags. In particular, it branches on Z' AND [(N AND V) OR (N' AND V')] = 1. BHI looks at the C and V flags. It branches if C' AND Z' = 1.Try out a couple of subtractions and keep track of your flags. Plug them into the branch formulas and you'll quickly see that the processor doesn't care whether your numbers are signed or unsigned. As long as you keep track and use the right branches, everything works perfectly.


What are the memory handling of machine-level languages?

If you have asked this question, note that it does not make sense. I'll humor you. If we're talking about a programming language like an assembler (assembly), memory is handled on a "per register" basis where a register is a block of memory. The physical size of the register is dependent on the computer's ISA (instruction set architecture). In a 32 bit system, each register would be capable of storing a 32 bit binary representation (2^32 - signed; please ask about signed and unsigned integers for more information).


What are the implications of the solar system in the different life forms on earth?

no idea at all


Do all device drivers need to be digitally signed to be installed on a computer running Windows 7?

Not really. I have installed drivers which is not digitally signed as long as it supports the Windows 7 system. It only warns you but you can proceed installing anyway.