negative-number

How to get the 2's complement value of a BigInteger of arbitrary length

落爺英雄遲暮 提交于 2019-12-23 06:14:37
问题 Is there a method in BigInteger to get the 2's complement value? For eg: if there is a BigInteger with a negative value BigInteger a = new BigInteger("-173B8EC504479C3E95DEB0460411962F9EF2ECE0D3AACD749BE39E1006FC87B8", 16); then I want to get the 2's complement in a BigInteger form BigInteger b = E8C4713AFBB863C16A214FB9FBEE69D0610D131F2C55328B641D61EFF9037848 I can subtract the first BigInteger from 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF to get the second

Why does right shifting -1 always gives -1 in PHP?

杀马特。学长 韩版系。学妹 提交于 2019-12-22 05:33:33
问题 I am trying to figure out why if I shift the negative integer -1 I always get -1, e.g.: echo -1 >> 64; // -1 echo -1 >> 5; // -1 echo -1 >> 43; // -1 echo -1 >> 1; // -1 Whatever second operand of the right shift is given, -1 remains -1... I do understand that when you perform a right shift you're actually doing this: x >> y = x / 2^y But in the case of x being -1, if so, I do: -1 >> 3 = -1 / 2^3 Shouldn't this value be -1/8 = -0.125? Thanks for the attention. 回答1: Bitwise shift operators don

Why does slice [:-0] return empty list in Python

流过昼夜 提交于 2019-12-22 04:36:25
问题 Stumbled upon something slightly perplexing today while writing some unittests: blah = ['a', 'b', 'c'] blah[:-3] # [] blah[:-2] # ['a'] blah[:-1] # ['a', 'b'] blah[:-0] # [] Can't for the life of me figure out why blah[:-0] # [] should be the case, the pattern definitely seems to suggest that it should be ['a', 'b', 'c'] . Can anybody help to shed some light on why that is the case? Haven't been able to find mention in the docs as to why that is the case. 回答1: -0 is 0 , and a slice that goes

Modulo of a negative number

余生长醉 提交于 2019-12-21 18:42:30
问题 Consider the following expression: (a - b) mod N Which of the following is equivalent to the above expression? 1) ((a mod N) + (-b mod N)) mod N 2) ((a mod N) - (b mod N)) mod N Also, how is (-b mod N) calculated, i.e., how is the mod of a negative number calculated? Thanks. 回答1: I don't want to bother you with some complex mathematical concepts, so i'll try to keep it simple. When we say that a = b (mod c), we simply say that a-b is a multiple of c. This means that when we want to know what

Python Argparse: Issue with optional arguments which are negative numbers

安稳与你 提交于 2019-12-20 10:19:31
问题 I'm having a small issue with argparse . I have an option xlim which is the xrange of a plot. I want to be able to pass numbers like -2e-5 . However this does not work - argparse interprets this is a positional argument. If I do -0.00002 it works: argparse reads it as a negative number. Is it possible to have able to read in -2e-3 ? The code is below, and an example of how I would run it is: ./blaa.py --xlim -2.e-3 1e4 If I do the following it works: ./blaa.py --xlim -0.002 1e4 The code:

Compare negative numbers in bash [duplicate]

混江龙づ霸主 提交于 2019-12-19 04:18:13
问题 This question already has answers here : Comparing numbers in Bash (8 answers) Closed last year . How can i accomplish number comparison involving negative numbers? if [[ "-2" > "-1" ]]; then echo "-2 >-1" else echo "-2 <=-1" fi I also tried if [ '-2' -lt '-1' ]; then but the condition always behaves as if -2 would be greater than -1. The comparisons work when i do not use negative numbers. I would like a solution in pure bash if possible. 回答1: Seems to work correctly: if [[ "-2" -gt "-1" ]];

Javascript negative number

。_饼干妹妹 提交于 2019-12-18 10:04:34
问题 I want to check if a number is negative. I'm searching for the easiest way , so a predefined javascript function would be the best but I didn't found yet anything, here is what I have so far but I don't think that this is a good way: function negative(number) { if (number.match(/^-\d+$/)) { return true; } else { return false; } } 回答1: Instead of writing a function to do this check, you should just be able to use this expression: (number < 0) Javascript will evaluate this expression by first

Replacing negative values in a model (system of ODEs) with zero

旧巷老猫 提交于 2019-12-18 04:59:10
问题 I'm currently working on solving a system of ordinary differential equations using deSolve , and was wondering if there's any way of preventing differential variable values from going below zero. I've seen a few other posts about setting negative values to zero in a vector, data frame, etc., but since this is a biological model (and it doesn't make sense for a T cell count to go negative), I need to stop it from happening to begin with so these values don't skew the results, not just replace

Format Negative numbers in parenthesis BUT NOT with $ symbol?

ぐ巨炮叔叔 提交于 2019-12-17 23:44:36
问题 I have seen all over the internet to format a NEGATIVE double value with a parenthesis WITH a $ symbol ie. currency type. I am looking for a .NET format string, to format 12345.67 = 12,345.67 -12345.67 = (12,345.67) 回答1: MSDN on conditional formatting to the rescue! You can specify up to three different sections of your format string at once, separating them with semicolons. If you specify two format string sections, the first is used for positive and zero values while the second is used for

1000 * 60 * 60 * 24 * 30 results in a negative number [duplicate]

↘锁芯ラ 提交于 2019-12-17 16:49:11
问题 This question already has answers here : Why do these two multiplication operations give different results? (2 answers) Closed 6 years ago . I'm attempting to calculate 30 days by multiplying milliseconds however the result continually ends up being a negative number for the value of days_30 and I'm not sure why. Any suggestions are greatly appreciated! CODE SNIPPET: // check to ensure proper time has elapsed SharedPreferences pref = getApplicationContext() .getSharedPreferences(