negative-number

Representation of negative numbers in C?

人走茶凉 提交于 2019-11-26 04:25:35
问题 How does C represent negative integers? Is it by two\'s complement representation or by using the MSB (most significant bit)? -1 in hexadecimal is ffffffff . So please clarify this for me. 回答1: ISO C ( C99 section 6.2.6.2/2 in this case but it carries forward to later iterations of the standard (a) ) states that an implementation must choose one of three different representations for integral data types, two's complement, ones' complement or sign/magnitude (although it's incredibly likely

Why prefer two's complement over sign-and-magnitude for signed numbers?

家住魔仙堡 提交于 2019-11-26 04:02:01
问题 I\'m just curious if there\'s a reason why in order to represent -1 in binary, two\'s complement is used: flipping the bits and adding 1? -1 is represented by 11111111 (two\'s complement) rather than (to me more intuitive) 10000001 which is binary 1 with first bit as negative flag. Disclaimer: I don\'t rely on binary arithmetic for my job! 回答1: It's done so that addition doesn't need to have any special logic for dealing with negative numbers. Check out the article on Wikipedia. Say you have

How does java do modulus calculations with negative numbers?

青春壹個敷衍的年華 提交于 2019-11-26 01:49:27
问题 Am I doing modulus wrong? Because in Java -13 % 64 is supposed to evaluate to -13 but I get 51 . 回答1: Both definitions of modulus of negative numbers are in use - some languages use one definition and some the other. If you want to get a negative number for negative inputs then you can use this: int r = x % n; if (r > 0 && x < 0) { r -= n; } Likewise if you were using a language that returns a negative number on a negative input and you would prefer positive: int r = x % n; if (r < 0) { r +=

Are there any non-twos-complement implementations of C?

六眼飞鱼酱① 提交于 2019-11-26 01:34:27
问题 As we all no doubt know, the ISO C standard (and C++ as well, I think, though I\'m more interested on the C side) allows three underlying representations of signed numbers: two\'s complement; ones\' complement; and sign/magnitude. Wikipedia\'s entry states that sign/magnitude is used on the IBM 7090 from the 60s, and that ones\' complement is used by the PDP-1, CDC 160A and UNIVAC 1100, all of which date back to the 60s as well. Are there any other implementations of C (or underlying hardware

How does java do modulus calculations with negative numbers?

一个人想着一个人 提交于 2019-11-26 01:24:59
Am I doing modulus wrong? Because in Java -13 % 64 is supposed to evaluate to -13 but I get 51 . Both definitions of modulus of negative numbers are in use - some languages use one definition and some the other. If you want to get a negative number for negative inputs then you can use this: int r = x % n; if (r > 0 && x < 0) { r -= n; } Likewise if you were using a language that returns a negative number on a negative input and you would prefer positive: int r = x % n; if (r < 0) { r += n; } Since "mathematically" both are correct: -13 % 64 = -13 (on modulus 64) -13 % 64 = 51 (on modulus 64)

The modulo operation on negative numbers in Python

末鹿安然 提交于 2019-11-26 00:59:21
问题 I\'ve found some strange behaviour in Python regarding negative numbers: >>> -5 % 4 3 Could anyone explain what\'s going on? 回答1: Unlike C or C++, Python's modulo operator ( % ) always return a number having the same sign as the denominator (divisor). Your expression yields 3 because (-5) % 4 = (-2 × 4 + 3) % 4 = 3. It is chosen over the C behavior because a nonnegative result is often more useful. An example is to compute week days. If today is Tuesday (day #2), what is the week day N days

Right shifting negative numbers in C

懵懂的女人 提交于 2019-11-26 00:55:35
问题 I have C code in which I do the following. int nPosVal = +0xFFFF; // + Added for ease of understanding int nNegVal = -0xFFFF; // - Added for valid reason Now when I try printf (\"%d %d\", nPosVal >> 1, nNegVal >> 1); I get 32767 -32768 Is this expected? I am able to think something like 65535 >> 1 = (int) 32767.5 = 32767 -65535 >> 1 = (int) -32767.5 = -32768 That is, -32767.5 is rounded off to -32768. Is this understanding correct? 回答1: It looks like your implementation is probably doing an

Right shifting negative numbers in C

别等时光非礼了梦想. 提交于 2019-11-25 19:40:43
I have C code in which I do the following. int nPosVal = +0xFFFF; // + Added for ease of understanding int nNegVal = -0xFFFF; // - Added for valid reason Now when I try printf ("%d %d", nPosVal >> 1, nNegVal >> 1); I get 32767 -32768 Is this expected? I am able to think something like 65535 >> 1 = (int) 32767.5 = 32767 -65535 >> 1 = (int) -32767.5 = -32768 That is, -32767.5 is rounded off to -32768. Is this understanding correct? It looks like your implementation is probably doing an arithmetic bit shift with two's complement numbers. In this system, it shifts all of the bits to the right and