signed

32-bit signed integer multiplication without using 64-bit data type

最后都变了- 提交于 2019-11-26 21:04:54
问题 I want to do 32-bit signed integer multiplication without using a 64-bit data type. My inputs are in Q1.31 (both) format. input1 = A32 (Ah Al) - higher, lower half's of A32 input2 = B32 (Bh Bl) - higher, lower half's of B32 Result should be in Q1.31 format, leave the overflow case. I need C code. Please provide the explanation with formats also. 回答1: Signed Q1.31 format is a fully fractional format capable of representing operands between -1 and almost +1. The scale factor is 2 31 . This

Why does -INT_MIN = INT_MIN in a signed, two's complement representation?

血红的双手。 提交于 2019-11-26 20:58:28
I still haven't found a reason why the lowest signed negative number doesn't have an equivalent signed positive number? I mean in a 3 digit binary number for simplicity 100 is -4? but we can't have a positive 4 in signed format because we can't. It overflows. So how do we know two's complement 1000 is -4 1000 0000 is -128 and so on? We have no original positive number One way to think about it is that signed, two's complement format works by assigning each bit a power of two, then flipping the sign of the last power of two. Let's look at -4, for example, which is represented as 100. This means

Why is a negative int greater than unsigned int? [duplicate]

泪湿孤枕 提交于 2019-11-26 18:57:40
This question already has an answer here: Comparison operation on unsigned and signed integers 7 answers int main(void) { unsigned int y = 10; int x = – 4; if (x > y) Printf("x is greater"); else Printf("y is greater"); getch(); return (0); } Output: x is greater I thought the output would be y is greater since it is unsigned. What's the reason behind this? Because the int value is promoted to an unsigned int . specifically 0xFFFFFFFC on a 32-bit machine, which as an unsigned int is 4294967292 , considerably larger than 10 C99 6.3.1.1-p2 If an int can represent all values of the original type

Why is 0 < -0x80000000?

≡放荡痞女 提交于 2019-11-26 18:08:48
I have below a simple program: #include <stdio.h> #define INT32_MIN (-0x80000000) int main(void) { long long bal = 0; if(bal < INT32_MIN ) { printf("Failed!!!"); } else { printf("Success!!!"); } return 0; } The condition if(bal < INT32_MIN ) is always true. How is it possible? It works fine if I change the macro to: #define INT32_MIN (-2147483648L) Can anyone point out the issue? Lundin This is quite subtle. Every integer literal in your program has a type. Which type it has is regulated by a table in 6.4.4.1: Suffix Decimal Constant Octal or Hexadecimal Constant none int int long int unsigned

Why do we define INT_MIN as -INT_MAX - 1? [duplicate]

不打扰是莪最后的温柔 提交于 2019-11-26 17:21:22
问题 This question already has an answer here: (-2147483648> 0) returns true in C++? 4 answers AFAIK this is a standard "idiom" # define INT_MIN (-INT_MAX - 1) # define INT_MAX 2147483647 Question: Why is the definition of INT_MIN not as -2147483648? 回答1: Because 2147483648 is a long value as it does not fit in an int (in common system with 32-bit int and 64-bit long , on system with 32-bit long it is of type long long ). So -2147483648 is of type long , not int . Remember in C, an unsuffixed

Difference between signed and unsigned on bitwise operations

☆樱花仙子☆ 提交于 2019-11-26 17:08:06
问题 Is there any difference between signed and unsigned variables on bitwise operations? For example,when dealing with unsigned numbers: AND 00000111, 00001101 will result 00000101. But what would happen when dealing with signed numbers? 回答1: Assuming 2's complement is used for signed numbers, operations that care about signedness (ie they are different for the signed and unsigned interpretation of a bitstring) are: division modulo right shift comparisons (except equality) double-width

What is the best way to work around the fact that ALL Java bytes are signed?

依然范特西╮ 提交于 2019-11-26 15:31:07
问题 In Java, there is no such thing as an unsigned byte. Working with some low level code, occasionally you need to work with bytes that have unsigned values greater than 128, which causes Java to interpret them as a negative number due to the MSB being used for sign. What's a good way to work around this? (Saying don't use Java is not an option) 回答1: When reading any single value from the array copy it into something like a short or an int and manually convert the negative number into the

Signedness of enum in C/C99/C++/C++x/GNU C/GNU C99

允我心安 提交于 2019-11-26 14:15:15
问题 Is enum type signed or unsigned? Is the Signedness of enums differ in C/C99/ANSI C/C++/C++x/GNU C/ GNU C99? Thanks 回答1: An enum is guaranteed to be represented by an integer, but the actual type (and its signedness) is implementation-dependent. You can force an enumeration to be represented by a signed type by giving one of the enumerators a negative value: enum SignedEnum { a = -1 }; In C++0x, the underlying type of an enumeration can be explicitly specified: enum ShortEnum : short { a }; (C

Unsigned hexadecimal constant in C?

时光总嘲笑我的痴心妄想 提交于 2019-11-26 09:39:53
问题 Does C treat hexadecimal constants (e.g. 0x23FE) and signed or unsigned int? 回答1: The number itself is always interpreted as a non-negative number. Hexadecimal constants don't have a sign or any inherent way to express a negative number. The type of the constant is the first one of these which can represent their value: int unsigned int long int unsigned long int long long int unsigned long long int 回答2: It treats them as int literals(basically, as signed int!). To write an unsigned literal

Can a pointer (address) ever be negative?

最后都变了- 提交于 2019-11-26 09:39:05
问题 I have a function that I would like to be able to return special values for failure and uninitialized (it returns a pointer on success). Currently it returns NULL for failure, and -1 for uninitialized, and this seems to work... but I could be cheating the system. IIRC, addresses are always positive, are they not? (although since the compiler is allowing me to set an address to -1, this seems strange). [update] Another idea I had (in the event that -1 was risky) is to malloc a char @ the