signed-integer

Unsigned Multiplication using Signed Multiplier

偶尔善良 提交于 2021-01-29 07:18:08
问题 I have been assigned with a task to perform unsigned multiplication using signed multiplier. Despite multiple attempts, I couldn't get it. Is it possible to do this? Code: #include <stdio.h> int main() { // Must be short int short int a=0x7fff; short int b=0xc000; unsigned int res1; signed int res2; //unsigned multiplier res1= (unsigned short int) a * (unsigned short int) b; //signed multiplier res2= (short int) a * (short int) b; printf("res1: 0x%x %d \n res2: 0x%x %d\n",res1,res1,res2,res2)

Assembly imul signed

依然范特西╮ 提交于 2021-01-27 17:51:56
问题 thx for help my question is about ax value received from code below? mov al,22h mov cl,0fdh imul cl Actual machine result: ff9a What I expected: 00:9a (by multiplying in binary) The first number is 22h so its 34 decimal its already unsigned the second number is fd in binary its goes like 11111101 so its signed that mean its like -3 so 22* -3 its 66; and -66 on signed 9a so why there is ff at the beginning 回答1: imul cl does AX = AL * CL , producing a full 16-bit signed product from 8-bit

How to implement arithmetic right shift in C

我怕爱的太早我们不能终老 提交于 2021-01-21 07:47:17
问题 Many lossless algorithms in signal processing require an evaluation of the expression of the form ⌊ a / 2 b ⌋, where a , b are signed ( a possibly negative, b non-negative) integers and ⌊·⌋ is the floor function. This usually leads to the following implementation. int floor_div_pow2(int numerator, int log2_denominator) { return numerator >> log2_denominator; } Unfortunately, the C standard states that the result of the >> operator is implementation-defined if the left operand has a signed

What is signed division(idiv) instruction?

房东的猫 提交于 2021-01-19 07:02:21
问题 In intel instruction, idiv(integer divsion) means signed division. I got the result of idiv , but I don't quite understand the result. - Example 0xffff0000 idiv 0xffff1100 - My wrong prediction As long as I know, quotient should be 0, and remainder should be 0xffff0000 and because... 0xffff0000 / 0xffff1100 = 0 0xffff0000 % 0xffff1100 = 0xffff0000 - However, the result was... Before idiv eax 0xffff0000 # dividend esi 0xffff1100 # divisor edx 0x0 After idiv eax 0xfffeedcc # quotient edx 0x7400

What is signed division(idiv) instruction?

谁说我不能喝 提交于 2021-01-19 07:01:26
问题 In intel instruction, idiv(integer divsion) means signed division. I got the result of idiv , but I don't quite understand the result. - Example 0xffff0000 idiv 0xffff1100 - My wrong prediction As long as I know, quotient should be 0, and remainder should be 0xffff0000 and because... 0xffff0000 / 0xffff1100 = 0 0xffff0000 % 0xffff1100 = 0xffff0000 - However, the result was... Before idiv eax 0xffff0000 # dividend esi 0xffff1100 # divisor edx 0x0 After idiv eax 0xfffeedcc # quotient edx 0x7400

Unreasonable behavior on mixing signed and unsigned integers

情到浓时终转凉″ 提交于 2020-01-30 02:30:10
问题 Motivated from a code snippet on this blog under "What happens when I mix signed and unsigned integers?" I decided to run it with few different values of signed and unsigned integers and observe the behaviour. Here is the original snippet (slightly modified, however intent is still same) #include <stdio.h> int main(void) { unsigned int a = 6; int b = -20; int c = (a+b > 6); unsigned int d = a+b; printf("<%d,%u>", c, d); } OUTPUT: <1,4294967282> Now when I run the same program for a = 6 and b

What is a difference between unsigned int and signed int in C?

北城以北 提交于 2019-12-17 17:39:40
问题 Consider these definitions: int x=5; int y=-5; unsigned int z=5; How are they stored in memory? Can anybody explain the bit representation of these in memory? Can int x=5 and int y=-5 have same bit representation in memory? 回答1: ISO C states what the differences are. The int data type is signed and has a minimum range of at least -32767 through 32767 inclusive. The actual values are given in limits.h as INT_MIN and INT_MAX respectively. An unsigned int has a minimal range of 0 through 65535

Is comparing an underflowed, unsigned integer to -1 well-defined?

限于喜欢 提交于 2019-12-03 08:29:40
问题 Consider the following † : size_t r = 0; r--; const bool result = (r == -1); Does the comparison whose result initialises result have well-defined behaviour? And is its result true , as I'd expect? This Q&A was written because I was unsure of two factors in particular. They may both be identified by use of the term "crucial[ly]" in my answer. † This example is inspired by an approach for loop conditions when the counter is unsigned: for (size_t r = m.size() - 1; r != -1; r--) 回答1: size_t r =

Is comparing an underflowed, unsigned integer to -1 well-defined?

限于喜欢 提交于 2019-12-02 22:14:53
Consider the following † : size_t r = 0; r--; const bool result = (r == -1); Does the comparison whose result initialises result have well-defined behaviour? And is its result true , as I'd expect? This Q&A was written because I was unsure of two factors in particular. They may both be identified by use of the term "crucial[ly]" in my answer. † This example is inspired by an approach for loop conditions when the counter is unsigned: for (size_t r = m.size() - 1; r != -1; r--) size_t r = 0; r--; const bool result = (r == -1); Strictly speaking, the value of result is implementation-defined. In

What is a difference between unsigned int and signed int in C?

99封情书 提交于 2019-11-28 04:41:20
Consider these definitions: int x=5; int y=-5; unsigned int z=5; How are they stored in memory? Can anybody explain the bit representation of these in memory? Can int x=5 and int y=-5 have same bit representation in memory? ISO C states what the differences are. The int data type is signed and has a minimum range of at least -32767 through 32767 inclusive. The actual values are given in limits.h as INT_MIN and INT_MAX respectively. An unsigned int has a minimal range of 0 through 65535 inclusive with the actual maximum value being UINT_MAX from that same header file. Beyond that, the standard