signed

convert ascii character to signed 8-bit integer python

做~自己de王妃 提交于 2019-12-05 11:12:42
This feels like it should be very simple, but I haven't been able to find an answer.. In a python script I am reading in data from a USB device (x and y movements of a USB mouse). it arrives in single ASCII characters. I can easily convert to unsigned integers (0-255) using ord. But, I would like it as signed integers (-128 to 127) - how can I do this? Any help greatly appreciated! Thanks a lot. Subtract 256 if over 127: unsigned = ord(character) signed = unsigned - 256 if unsigned > 127 else unsigned Alternatively, repack the byte with the struct module: from struct import pack, unpack signed

How does adding MIN_VALUE compare integers as unsigned?

瘦欲@ 提交于 2019-12-05 05:46:48
In Java the int type is signed, but it has a method that compares two ints as if they were unsigned: public static int compareUnsigned(int x, int y) { return compare(x + MIN_VALUE, y + MIN_VALUE); } It adds Integer.MIN_VALUE to each argument, then calls the normal signed comparison method, which is: public static int compare(int x, int y) { return (x < y) ? -1 : ((x == y) ? 0 : 1); } How does adding MIN_VALUE to each argument magically make the comparison unsigned? This technique works for any size of integer, but I'll use an 8-bit byte-sized integer to explain, because the numbers are smaller

16 bit hex string to signed int in Java

我是研究僧i 提交于 2019-12-05 00:35:38
I have a string in Java representing a signed 16-bit value in HEX. This string can by anything from "0000" to "FFFF" . I use Integer.parseInt("FFFF",16) to convert it to an integer. However, this returns an unsigned value ( 65535 ). I want it to return a signed value. In this particular example "FFFF" should return -1 . How can I achieve this? Since its a 16-bit value I thought of using Short.parseShort("FFFF",16) but that tells me that I am out of range. I guess parseShort() expects a negative sign. You can cast the int returned from Integer.parseInt() to a short: short s = (short) Integer

Invalid conversion from unsigned char* to char*

送分小仙女□ 提交于 2019-12-05 00:21:40
Here is a code - 1 int main(int argc, char *argv[]) 2 { 3 signed char S, *psc; 4 unsigned char U, *pusc; 5 char C, *pc; 6 7 C = S; 8 C = U; 9 10 pc = psc; 11 pc = pusc; 12 13 return 0; 14 } $ gcc test.cpp -o a test.cpp: In function ‘int main(int, char**)’: test.cpp:10:7: error: invalid conversion from ‘signed char*’ to ‘char*’ [-fpermissive] test.cpp:11:7: error: invalid conversion from ‘unsigned char*’ to ‘char*’ [-fpermissive] This is compiled on gcc version 4.6.3 on Ubuntu 12.10 on an Intel 32-bit machine. Considering that char type is unsigned char on x86. - If assignments on line 7 and 8

When I calculate a large factorial, why do I get a negative number?

痴心易碎 提交于 2019-12-04 19:29:59
So, simple procedure, calculate a factorial number. Code is as follows. int calcFactorial(int num) { int total = 1; if (num == 0) { return 0; } for (num; num > 0; num--) { total *= num; } return total; } Now, this works fine and dandy (There are certainly quicker and more elegant solutions, but this works for me) for most numbers. However when inputting larger numbers such as 250 it, to put it bluntly, craps out. Now, the first couple factorial "bits" for 250 are { 250, 62250, 15126750, 15438000, 3813186000 } for reference. My code spits out { 250, 62250, 15126750, 15438000, -481781296 } which

double precision integer subtraction with 32-bit registers(MIPS)

强颜欢笑 提交于 2019-12-04 18:21:22
I am learning computer arithmetic. The book I use(Patterson and Hennessey) lists the below question. Write mips code to conduct double precision integer subtraction for 64-bit data. Assume the first operand to be in registers $t4(hi) and $t5(lo), second in $t6(hi) and $t7(lo). My solution to the answer is sub $t3, $t5, $t7 # Subtract lo parts of operands. t3 = t5 - t7 sltu $t2, $t5, $t7 # If the lo part of the 1st operand is less than the 2nd, # it means a borrow must be made from the hi part add $t6, $t6, $t2 # Simulate the borrow of the msb-of-low from lsb-of-high sub $t2, $t4, $t6 #

How computer distinguish an integer is signed or unsigned?

谁都会走 提交于 2019-12-04 10:40:52
问题 Two's complements is set to make it easier for computer to compute the substraction of two numbers. But how computer distinguish an integer is signed integer or unsigned integer? It's just 0 and 1 in its memory. For exmaple, 1111 1111 in the computer memory may represent number 255 but also can represent -1. 回答1: Signed and unsigned use the same data, but different instructions. The computer stores signed and unsigned integers as the same data. I.e. 255 and -1 are the same bits. However, you

Android Volley Signed Apk Issue

不羁的心 提交于 2019-12-04 10:05:16
I am working with an application where I am using volley plus with android studio. I am using volley plus gradle version 'dev.dworks.libs:volleyplus:+' gradle. When I make signed apk of my app my build process terminated with some warning and errors with runProguard true. There is any solution to solve this. Warning:com.android.volley.toolbox.HttpClientStack$HttpPatch: can't find superclass or interface org.apache.http.client.methods.HttpEntityEnclosingRequestBase Warning:com.android.volley.toolbox.multipart.MultipartEntity: can't find superclass or interface org.apache.http.entity

C : Convert signed to unsigned

你离开我真会死。 提交于 2019-12-04 06:34:20
问题 Actually I've (probably) a "simple" problem. So I don't know how to cast a signed integer to an unsigned integer. My code : signed int entry = 0; printf("Decimal Number : "); scanf("%d", &entry); unsigned int uEntry= (unsigned int) entry; printf("Unsigned : %d\n", uEntry); If I send the unsigned value to the console (see my last code line), I always get back an signed integer. Can you help me? Thanks a lot! Kind regards, pro 回答1: printf("Unsigned : %u\n", uEntry); // ^^ You must use the %u

Why must loop variables be signed in a parallel for?

流过昼夜 提交于 2019-12-04 06:16:23
I'm just learning OpenMP from online tutorials and resources. I want to square a matrix (multiply it with itself) using a parallel for loop. In IBM compiler documentation , I found the requirement that "the iteration variable must be a signed integer." Is this also true in the GCC implementation? Is it specified in the OpenMP standard? If so, is there a reason for this requirement? (It doesn't matter much as the expected dimensions are far smaller than INT_MAX , but it does cost me some casts.) According to OpenMP 3.0 specification: http://www.openmp.org/mp-documents/spec30.pdf , for variable