signed

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

心不动则不痛 提交于 2019-11-27 22:38:20
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. 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 means that when each Q1.31 operand is stored in a 32-bit signed integer, we can generate the Q1.31 product by

Signed vs. unsigned integers for lengths/counts

喜你入骨 提交于 2019-11-27 20:07:39
For representing a length or count variable, is it better to use signed or unsigned integers? It seems to me that C++ STL tends to prefer unsigned ( std::size_t , like in std::vector::size() , instead C# BCL tends to prefer signed integers (like in ICollection.Count . Considering that a length or a count are non-negative integers, my intuition would choose unsigned ; but I fail to understand why the .NET designers chose signed integers. What is the best approach? What are the pros and cons of each one? C++ uses unsigned values because they need the full range. On a 32-bit system, the language

How to perform unsigned to signed conversion in Java?

心不动则不痛 提交于 2019-11-27 18:38:30
问题 Say I read these bytes: "6F D4 06 40" from an input device. The number is a longitude reading in MilliArcSeconds format. The top bit (0x80000000) is basically always zero and is ignored for this question. I can easily convert the bytes to an unsigned integer: 1876166208 But how do I convert that unsigned value into its final form of 31-bit signed-integer? So far all I've come up with is: if value & 0x40000000 then it's actually negative, need to convert it If it's negative, strip the top bit

Why does this if condition fail for comparison of negative and positive integers [duplicate]

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-27 16:08:21
This question already has an answer here: sizeof() operator in if-statement 5 answers #include <stdio.h> int arr[] = {1,2,3,4,5,6,7,8}; #define SIZE (sizeof(arr)/sizeof(int)) int main() { printf("SIZE = %d\n", SIZE); if ((-1) < SIZE) printf("less"); else printf("more"); } The output after compiling with gcc is "more" . Why the if condition fails even when -1 < 8 ? The problem is in your comparison: if ((-1) < SIZE) sizeof typically returns an unsigned long , so SIZE will be unsigned long , whereas -1 is just an int . The rules for promotion in C and related languages mean that -1 will be

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

假装没事ソ 提交于 2019-11-27 15:47:01
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? 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 decimal integer constant is of the first type int , long or long long where it can be represented. Also in C

How can deploy signed APK when click Run in Android Studio?

坚强是说给别人听的谎言 提交于 2019-11-27 15:39:24
问题 I am developing an app which uses Google APIs. I have created credentials at "Google Developers Console". If I creates a signed APK, and run it on the phone, there is no problem. The problem is, while I developing the app, when I click RUN button, it deploys an unsigned version of the app on the phone. Thus the application does not work. How can I set Android Studio to make it deploy signed APK on the phone when click RUN button? 回答1: Add these values to your .gradle: signingConfigs{ key{

Would it break the language or existing code if we'd add safe signed/unsigned compares to C/C++?

跟風遠走 提交于 2019-11-27 13:54:06
问题 After reading this question on signed/unsigned compares (they come up every couple of days I'd say): Signed / unsigned comparison and -Wall I wondered why we don't have proper signed unsigned compares and instead this horrible mess? Take the output from this small program: #include <stdio.h> #define C(T1,T2)\ {signed T1 a=-1;\ unsigned T2 b=1;\ printf("(signed %5s)%d < (unsigned %5s)%d = %d\n",#T1,(int)a,#T2,(int)b,(a<b));}\ #define C1(T) printf("%s:%d\n",#T,(int)sizeof(T)); C(T,char);C(T

What is zero-width bit field [duplicate]

纵然是瞬间 提交于 2019-11-27 12:24:05
问题 Possible Duplicate: Practical Use of Zero-Length Bitfields Why some structures have zero-width bit fields, and why is it required? struct foo { int a:3; int b:2; int :0; // Force alignment to next boundary. int c:4; int d:3; }; int main() { int i = 0xFFFF; struct foo *f = (struct foo *)&i; printf("a=%d\nb=%d\nc=%d\nd=%d\n", f->a, f->b, f->c, f->d); return 0; } The output of above program is manav@os-team:~/programs/test$ ./a.out a=-1 b=-1 c=-8 d=0 Please explain why these values are negative,

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

≯℡__Kan透↙ 提交于 2019-11-27 11:17:55
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) 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 positive value it should be. byte[] foobar = ..; int value = foobar[10]; if (value < 0) value += 256 // Patch up

What is the difference between “int” and “uint” / “long” and “ulong”?

谁说胖子不能爱 提交于 2019-11-27 09:27:03
问题 I know about int and long (32-bit and 64-bit numbers), but what are uint and ulong ? 回答1: The primitive data types prefixed with "u" are unsigned versions with the same bit sizes. Effectively, this means they cannot store negative numbers, but on the other hand they can store positive numbers twice as large as their signed counterparts. The signed counterparts do not have "u" prefixed. The limits for int (32 bit) are: int: –2147483648 to 2147483647 uint: 0 to 4294967295 And for long (64 bit):