twos-complement

Calculate two's complement checksum of hexadecimal string

萝らか妹 提交于 2019-12-01 07:36:55
I have a string "0AAE0000463130004144430000" and I need to calculate the two's complement checksum of the hex bytes that make up the string. The formula for the example string above is Sum the values: 0A + AE + 00 + 00 + 46 + 31 + 30 + 00 + 41 + 44 + 43 + 00 + 00 = 27 (discard the overflow) Subtract the result from 0x100 = 0xD9 D9 is the correct checksum for this example, but I am having trouble getting the two digit hex values parsed out of the string in C#. My current code is below: string output = "0AAE0000463130004144430000"; long checksum = 0; char[] outputBytes = output.TrimStart(':')

sra(shift right arithmetic) vs srl (shift right logical)

只谈情不闲聊 提交于 2019-12-01 06:26:42
Please take a look at these two pieces of pseudo-assembly code: 1) li $t0,53 sll $t1,$t0,2 srl $t2,$t0,2 sra $t3,$t0,2 print $t1 print $t2 print $t3 2) li $t0,-53 sll $t1,$t0,2 srl $t2,$t0,2 sra $t3,$t0,2 print $t1 print $t2 print $t3 in the first case the output is: 212 13 13 in the latter is: -212 107374... -14 But shouldn't : sra (-53) = - (srl 53) ? -53 = 1111111111001011 sra 2 1111111111110010(11) = -14 ^^ ^^ sign dropped extension Because the extra bits are simply dropped for both positive and negative results, the result is always rounded down if you view the shift as a division. 53 sra

Calculate two's complement checksum of hexadecimal string

混江龙づ霸主 提交于 2019-12-01 04:30:22
问题 I have a string "0AAE0000463130004144430000" and I need to calculate the two's complement checksum of the hex bytes that make up the string. The formula for the example string above is Sum the values: 0A + AE + 00 + 00 + 46 + 31 + 30 + 00 + 41 + 44 + 43 + 00 + 00 = 27 (discard the overflow) Subtract the result from 0x100 = 0xD9 D9 is the correct checksum for this example, but I am having trouble getting the two digit hex values parsed out of the string in C#. My current code is below: string

longitude reading measured in degrees with a 1x10^-7 degree lsb, signed 2’s complement

谁都会走 提交于 2019-11-30 15:41:27
I am receiving data from a gps unit via a udp packet. Lat/Lng values are in hex. Example Data 13BF71A8 = Latitude (33.1313576) BA18A506 = Longitude (-117.2790010) The documentation explains that longitude/latitude readings are measured in degrees with a 1x10^-7 degree lsb, signed 2’s complement. For the Latitude I can convert using this formula: 13BF71A8 = 331313576 * 0.0000001 = 33.1313576 This code works for Lat but not for Lng: function convertLat(h){ var latdec = parseInt(h,16); var lat = latdec * 0.0000001; return lat; } console.log("LAT: " + convertLat("13BF71A8")); I am having trouble

Why we need to add 1 while doing 2's complement

你离开我真会死。 提交于 2019-11-30 14:25:22
问题 The 2's complement of a number which is represented by N bits is 2^N-number. For example: if number is 7 (0111) and i'm representing it using 4 bits then, 2's complement of it would be (2^N-number) i.e. (2^4 -7)=9(1001) 7==> 0111 1's compliment of 7==> 1000 1000 + 1 ------------- 1001 =====> (9) While calculating 2's complement of a number, we do following steps: 1. do one's complement of the number 2. Add one to the result of step 1. I understand that we need to do one's complement of the

how to do two complement multiplication and division of integers?

放肆的年华 提交于 2019-11-30 12:07:04
问题 I have read this post on binary multiplication using two complement. but it is not very clear to me. Even I have difficulty understanding the wiki article on this. I want to know how to go about calculating multiplications of negative numbers: eg: -1 with -7 should give 7. A 4-bit, 2's complement of -1 is : 1111 A 4-bit, 2's complement of -7 is : 1001 some step-wise way of calculating the multiplication will be helpful. No article I came across talks about division. How to approach this? 回答1:

Why we need to add 1 while doing 2's complement

℡╲_俬逩灬. 提交于 2019-11-30 10:14:02
The 2's complement of a number which is represented by N bits is 2^N-number. For example: if number is 7 (0111) and i'm representing it using 4 bits then, 2's complement of it would be (2^N-number) i.e. (2^4 -7)=9(1001) 7==> 0111 1's compliment of 7==> 1000 1000 + 1 ------------- 1001 =====> (9) While calculating 2's complement of a number, we do following steps: 1. do one's complement of the number 2. Add one to the result of step 1. I understand that we need to do one's complement of the number because we are doing a negation operation. But why do we add the 1? This might be a silly question

Why byte b = (byte) 0xFF is equals to integer -1?

笑着哭i 提交于 2019-11-30 07:04:17
问题 Why byte b = (byte) 0xFF is equal to integer -1 ? Ex: int value = byte b = (byte) 0xFF; System.out.println(value); it will print -1 ? 回答1: Bytes are signed in Java. In binary 0x00 is 0, 0x01 is 1 and so on but all 1s (ie 0xFF) is -1, 0xFE is -2 and so on. See Two's complement, which is the binary encoding mechanism used. 回答2: b is promoted to an int in determining which overload of system.out.println to call. All bytes in Java are signed. The signed byte 0xff represents the value -1 . This is

Convert binary two's complement data into integer in objective-c

杀马特。学长 韩版系。学妹 提交于 2019-11-30 06:01:07
问题 I have some binary data (twos complement) coming from an accelerometer and I need to convert it to an integer. Is there a standard library function which does this, or do I need to write my own code? For example: I receive an NSData object from the acclerometer, which when converted to hex looks like this: C0088001803F Which is a concatenation of 3 blocks of 2-byte data: x = C008 y = 8001 z = 803F Focussing on the x-axis only: hex = C008 decimal = 49160 binary = 1100000000001000 twos

How to represent a negative number with a fraction in 2's complement?

此生再无相见时 提交于 2019-11-30 04:54:52
So I want to represent the number -12.5 . So 12.5 equals to: 001100.100 If I don't calculate the fraction then it's simple, -12 is: 110100 But what is -12.5? is it 110100.100 ? How can I calculate this negative fraction? With decimal number systems, each number position (or column) represents (reading a number from right to left): units (which is 10^0), tens (i.e. 10^1),hundreds (i.e. 10^2), etc. With unsigned binary numbers, the base is 2, thus each position becomes (again, reading from right to left): 1 (i.e. 2^0) ,2 (i.e. 2^1), 4 (i.e. 2^2), etc. For example 2^2 (4), 2^1 (2), 2^0 (1). In