long-integer

(a * b) / c MulDiv and dealing with overflow from intermediate multiplication

爱⌒轻易说出口 提交于 2019-12-04 01:04:39
问题 I need to do the following arithmetic: long a,b,c; long result = a*b/c; While the result is guaranteed to fit in long , the multiplication is not, so it can overflow. I tried to do it step by step (first multiply and then divide) while dealing with the overflow by splitting the intermediate result of a*b into an int array in size of max 4 ( much like the BigInteger is using its int[] mag variable). Here I got stuck with the division. I cannot get my head around the bitwise shifts required to

Representing a 64 bit integer in GNU/Linux

梦想的初衷 提交于 2019-12-04 00:04:12
问题 I am using Ubuntu 10.10 (64 bit) with gcc and I wanted to use a 64 bit integer in my C++ program. On my system the outputs of sizeof(long) , sizeof(long long int) and sizeof(int64_t) are all 8 bytes (64 bits). Which qualifier ( long , long long , or int64_t ) would you recommend for using 64 bit integers? 回答1: int64_t -- This is because it is the most portable representation. The other two could be represented differently on other machines. 回答2: int64_t. If you need 64 bits, declare it

Alternative to BigInteger.ModPow(); in C#

穿精又带淫゛_ 提交于 2019-12-03 23:18:19
i'm looking for an alternative to the BigInteger package of C# which has been introduced with NET 4.x. The mathematical operations with this object are terribly slow, I guess this is caused by the fact that the arithmetics are done on a higher level than the primitive types - or badly optimized, whatever. Int64/long/ulong or other 64bit-numbers are way to small and won't calculate correctly - I'm talking about 64bit-integer to the power of 64-bit integers. Hopefully someone can suggest my something. Thanks in advance. Honestly, if you have extremely large numbers and need to do heavy

java.lang.ClassCastException: java.math.BigInteger cannot be cast to java.lang.Long

人盡茶涼 提交于 2019-12-03 21:10:16
问题 I want to return number of rows using native sql. But console says me java.math.BigInteger cannot be cast to java.lang.Long . What's wrong? This is my method: public Long getNumRows(Integer id){ Session session = null; session = this.sessionFactory.getCurrentSession(); Query query = session .createSQLQuery("SELECT COUNT(*) FROM controllnews WHERE news_id=" + id + ";"); List firstResult = query.list(); return (Long) firstResult.get(0); } 回答1: Use BigInteger#longValue() method, instead of

How to create a 64 bit Unique Integer in Java

谁都会走 提交于 2019-12-03 20:29:40
I need to create a 64 bit unique integer in Java so that collision chances are low. The system is not distributed, so collisions between different computers are not a problem. Is there any way, we can create a 64 bit integer in Java which is always Unique? As of now I am using - long number = System.nanoTime(); Is this the right way to generate 64 bit Unique Integer in Java or is there anything else I can try? UPDATE:- How about doing this way? Will this be unique? UUID number = UUID.randomUUID(); long uniqueNumber = number.timestamp(); Peter Lawrey If you need the numbers to be unique in one

Possible Lossy conversion from long to int

亡梦爱人 提交于 2019-12-03 20:28:10
问题 I wish to enter one int and another long ex: 1 and 1000000000, and now I wish to create an array of size 1000000000. And then at each index of array, store int val, ex: arr[100000000] = 4 . When I'm trying to do this Netbeans shows me an error in this line: arr = new long[y+1]` and `arr[j] = 0` "Possible Lossy conversion from long to int". Here's my code:- public static void main(String[] args) throws IOException { BufferedReader s = new BufferedReader(new InputStreamReader(System.in));

How do you get an unsigned long out of a string?

ぃ、小莉子 提交于 2019-12-03 15:50:09
What's the safest and best way to retrieve an unsigned long from a string in C++? I know of a number of possible methods. First, converting a signed long taken from atol. char *myStr; // Initalized to some value somehow. unsigned long n = ((unsigned)atol(myStr)); The obvious problem with this is, what happens when the value stored in myStr is larger than a signed long can contain? What does atol retrieve? The next possibility is to use strtoul. char *myStr; // Initalized to some value somehow. unsigned long n = strtoul(myStr, 0, 10); However, this is a little over complicated for my needs. I'd

Python 256bit Hash function with number output

北战南征 提交于 2019-12-03 11:18:40
I need a Hash function with a 256bit output (as long int). First I thought I could use SHA256 from the hashlib but it has an String Output and I need a number to calculate with. Converting the 32 Byte String to a long would work also but I didn't find anything. In struct there is a unpack function but this only works for 8 Byte long types and not for longer longs. How about: >>> import hashlib >>> h = hashlib.sha256('something to hash') >>> h.hexdigest() 'a3899c4070fc75880fa445b6dfa44207cbaf924a450ce7175cd8500e597d3ec1' >>> n = int(h.hexdigest(),base=16) >>> print n

Why not use long for all integer values

懵懂的女人 提交于 2019-12-03 11:02:19
In my Java class we have just learned about of each of the following primitive data types: byte short int long Since the long data type contains the most bits, wouldn't it make sense to exclusively use the long data type to avoid restrictions? Questions Is there a particular downside to only using the long data type? Does it make sense to use for example, an int data type, instead of a long data type? Jordi Castilla Does it make sense to use for example, an int data type, instead of a long data type? ABSOLUTELY YES. MEMORY / DISK USAGE Using only one variable or two you won't see difference of

How could I convert data from string to long in c#

人走茶凉 提交于 2019-12-03 08:52:13
问题 How could i convert data from string to long in C#? I have data String strValue[i] ="1100.25"; now i want it in long l1; 回答1: Convert.ToInt64("1100.25") Method signature from MSDN: public static long ToInt64( string value ) 回答2: If you want to get the integer part of that number you must first convert it to a floating number then cast to long. long l1 = (long)Convert.ToDouble("1100.25"); You can use Math class to round up the number as you like, or just truncate... Math.Round Math.Ceil 回答3: