Home › Forums › C-programming.. › negative numbers, 2’s complement, signed/unsigned int/char ..
Tagged: signed unsigned twos complement
- This topic has 2 replies, 1 voice, and was last updated 12 years ago by
Humble.
-
AuthorPosts
-
Humble
KeymasterYep, this is always confusing .. Isnt it ? main reason being it depends on compiler and machine..
Any way lets think about gcc as the compiler. The unsigned or signed does matter to compiler.. how-ever the data representation in memory does not really worry about it. GCC represents the negative integers in 2’s complement form.
The +4 is represented as 0000 0100
The -4 is represented in 2’s complement form. To get 2s complement notation of an integer, first write the number in binary [1] , then invert the digits [2] , and add one to the result [3].
[1] 0000 0100 [2] 1111 1011 [3] 1111 1011 + 1 -------------- 1111 1100 -> -4 The MSB ( most significant bit) in the signed integer represent the sign of the integer.Humble
KeymasterThis reply has been marked as private.Humble
KeymasterIts always a doubt that whats default type for int and char . The default type for int is taken as signed.
How-ever char can be unsigned or signed .
/* Minimum and maximum values achar' can hold. */ # ifdef __CHAR_UNSIGNED__ # define CHAR_MIN 0 # define CHAR_MAX UCHAR_MAX # else # define CHAR_MIN SCHAR_MIN # define CHAR_MAX SCHAR_MAX # endif ` If you are with gcc compiler, the default is signed.. You can modify that with -funsigned-char option of gcc..
-funsigned-char Let the type "char" be unsigned, like "unsigned char". Each kind of machine has a default for what "char" should be. It is either like "unsigned char" by default or like "signed char" by default. Ideally, a portable program should always use "signed char" or "unsigned char" when it depends on the signedness of an object. But many programs have been written to use plain "char" and expect it to be signed, or expect it to be unsigned, depending on the machines they were written for. This option, and its inverse, let you make such a program work with the opposite default. The type "char" is always a distinct type from each of "signed char" or "unsigned char", even though its behavior is always just like one of those two. -fsigned-char Let the type "char" be signed, like "signed char". Note that this is equivalent to -fno-unsigned-char, which is the negative form of -funsigned-char. Likewise, the option -fno-signed-char is equivalent to -funsigned-char.
-
AuthorPosts