long-integer

Max HEX value for long type

血红的双手。 提交于 2019-12-02 02:04:38
问题 I have ported Java code to C#. Could you please explain why I have compile-time error in the follow line (I use VS 2008): private long l = 0xffffffffffffffffL; // 16 'f' got here Cannot convert source type ulong to target type long I need the same value here as for origin Java code. 回答1: Assuming you aren't worried about negative values, you could try using an unsigned long: private ulong l = 0xffffffffffffffffL; In Java the actual value of l would be -1 , because it would overflow the 2^63 -

Max HEX value for long type

百般思念 提交于 2019-12-02 01:47:42
I have ported Java code to C#. Could you please explain why I have compile-time error in the follow line (I use VS 2008): private long l = 0xffffffffffffffffL; // 16 'f' got here Cannot convert source type ulong to target type long I need the same value here as for origin Java code. Assuming you aren't worried about negative values, you could try using an unsigned long : private ulong l = 0xffffffffffffffffL; In Java the actual value of l would be -1 , because it would overflow the 2^63 - 1 maximum value, so you could just replace your constant with -1 . Java doesn't mind if a constant

what are default integer values?

醉酒当歌 提交于 2019-12-02 00:42:46
问题 I read somewhere that default floating point values like 1.2 are double not float . So what are default integer values like 6 , are they short , int or long ? 回答1: The type of integer literals given in base 10 is the first type in the following list in which their value can fit: int long int long long int For octal and hexadecimal literals, unsigned types will be considered as well, in the following order: int unsigned int long int unsigned long int long long int unsigned long long int You

Java: Why does “long” number get negative?

試著忘記壹切 提交于 2019-12-01 21:28:26
I have this code: long i = 0; while (true) { i += 10*i + 5; System.out.println(i); Thread.sleep(100); } Why does the long i get negative after a few prints? If the range is exceeded, shouldn't an error occur? Slimu Java doesn't throw an error if you increase a number after its maximum value. If you wish to have this behaviour, you could use the Math.addExact(long x, long y) method from Java 8. This method will throw an ArithmeticException if you pass the Long.MAX_VALUE . The reason why Java doesn't throw an exception and you receive negative numbers has to do with the way numbers are stored.

Converting a four character string to a long

两盒软妹~` 提交于 2019-12-01 20:31:41
I want to convert a four character string (i.e. four characters) into a long (i.e. convert them to ASCII codes and then put them into the long). As I understand it, this is done by writing the first character to the first byte of the long, the second to the adjacent memory location, and so on. But I don't know how to do this in C++. Can someone please point me in the right direction? Thanks in advance. Here's your set of four characters: const unsigned char buf[4] = { 'a', '0', '%', 'Q' }; Now we assemble a 32-bit unsigned integer: const uint32_t n = (buf[0]) | (buf[1] << 8) | (buf[2] << 16) |

Is it possible to store 2 32-bit values in one long int variable?

白昼怎懂夜的黑 提交于 2019-12-01 19:08:52
问题 I want to store two 32-bit values in a single long int variable. How would you do this on a 32-bit OS using C? Is it possible to store the data in a single long long variable? If so, how is that done? 回答1: Assuming a long is 64 bits on your platform, int v1 = 123; int v2 = 456; long val = v1 << 32 | v2; 回答2: Use an uint64_t and bitwise operators. uint64_t i64; uint32_t a32, b32; // Be carefull when shifting the a32. // It must be converted to a 64 bit value or you will loose the bits //

What to do when you need to store a (very) large number?

你说的曾经没有我的故事 提交于 2019-12-01 15:13:49
问题 I am trying to do a Project Euler problem but it involves adding the digits of a very large number. (100!) Using Java int and long are too small. Thanks for any suggestions 回答1: Class BigInteger looks like it might be what you are looking for. 回答2: Use BigInteger. Here is an example from the book Java Examples in a Nutshell that involves computing factorials, with caching. import java.math.BigInteger; import java.util.ArrayList; /* * Copyright (c) 2000 David Flanagan. All rights reserved. *

Proper way of converting string to long int in PHP

对着背影说爱祢 提交于 2019-12-01 13:51:25
问题 I tried (int) "4209531264" and intval("4209531264") but sadly all I get is 2147483647 (I realize this is because of 32 bit architecture or some php dependencies or something). I came up with "4209531264" + 0 which returns the correct result but it is surprising to see it working since it is beyond maxint. but the real question: is this the "right way" of converting string to long? edit: (float) that is. thanks for the comments! eye opening! 回答1: As long as you are not very particular about

Java 8: Why can't I parse this binary string into a long?

旧时模样 提交于 2019-12-01 12:15:15
Long story short, I was messing around with some basic genetic algorithm stuff in Java. I was using a long to store my genes, but I was using binary strings for readability while debugging. I came across an odd situation where I couldn't parse some binary strings that start with a 1 (I don't know if this is always the case, but it seems to be consistent with strings of 64 characters in length). I was able to replicate this with the following example: String binaryString = Long.toBinaryString(Long.MIN_VALUE); long smallestLongPossibleInJava = Long.parseLong(binaryString, 2); Which will throw

Java 8: Why can't I parse this binary string into a long?

守給你的承諾、 提交于 2019-12-01 11:21:35
问题 Long story short, I was messing around with some basic genetic algorithm stuff in Java. I was using a long to store my genes, but I was using binary strings for readability while debugging. I came across an odd situation where I couldn't parse some binary strings that start with a 1 (I don't know if this is always the case, but it seems to be consistent with strings of 64 characters in length). I was able to replicate this with the following example: String binaryString = Long.toBinaryString