long-integer

Can I convert long to int?

别来无恙 提交于 2019-11-27 05:26:39
问题 I want to convert long to int . If the value of long > int.MaxValue , I am happy to let it wrap around. What is the best way? 回答1: Just do (int)myLongValue . It'll do exactly what you want (discarding MSBs and taking LSBs) in unchecked context (which is the compiler default). It'll throw OverflowException in checked context if the value doesn't fit in an int : int myIntValue = unchecked((int)myLongValue); 回答2: Convert.ToInt32(myValue); Though I don't know what it will do when it's greater

Convert long to byte array and add it to another array

旧城冷巷雨未停 提交于 2019-11-27 05:16:19
问题 I want to change a values in byte array to put a long timestamp value in in the MSBs. Can someone tell me whats the best way to do it. I do not want to insert values bit-by-bit which I believe is very inefficient. long time = System.currentTimeMillis(); Long timeStamp = new Long(time); byte[] bArray = new byte[128]; What I want is something like: byte[0-63] = timeStamp.byteValue(); Is something like this possible . What is the best way to edit/insert values in this byte array. since byte is a

What's the difference between unsigned long/long/int in c/c++?

做~自己de王妃 提交于 2019-11-27 02:36:13
问题 It seems all of them take 4 bytes of space, so what's the difference? 回答1: First of all, the size of int/long is unspecified. So on your compiler, an int and a long might be the same, but this isn't universal across compilers. As for the difference between unsigned long and long : Assuming 4 bytes, a long has the range of -2,147,483,648 to 2,147,483,647 . An unsigned long has the range of 0 to 4,294,967,295 . One other difference is with overflow. For a signed type, an overflow has

What is the argument for printf that formats a long?

≯℡__Kan透↙ 提交于 2019-11-27 02:34:57
The printf function takes an argument type, such as %d or %i for a signed int . However, I don't see anything for a long value. postfuturist Put an l (lowercased letter L) directly before the specifier. unsigned long n; long m; printf("%lu %ld", n, m); Blorgbeard I think you mean: unsigned long n; printf("%lu", n); // unsigned long or long n; printf("%ld", n); // signed long On most platforms, long and int are the same size (32 bits). Still, it does have its own format specifier: long n; unsigned long un; printf("%ld", n); // signed printf("%lu", un); // unsigned For 64 bits, you'd want a long

Why can't I assign a 'long' a value of 4 billion?

扶醉桌前 提交于 2019-11-27 02:30:15
问题 I'm trying to declare a long value in Java, which unfortunately does not work. This is my code. It results in the following error message: "The literal 4294967296 of type int is out of range". long bytes = 4294967296; I need this value to make a file filter that filters out files that are bigger than 4294967296 bytes (4GB). The other way round works without any issues ( long size = file.length() ) with every file size, which is why I can't figure out why my declaration is not working. 回答1:

Java creating byte array whose size is represented by a long

眉间皱痕 提交于 2019-11-27 01:57:37
I'm trying to create a byte array whose size is of type long . For example, think of it as: long x = _________; byte[] b = new byte[x]; Apparently you can only specify an int for the size of a byte array. Before anyone asks why I would need a byte array so large, I'll say I need to encapsulate data of message formats that I am not writing, and one of these message types has a length of an unsigned int ( long in Java). Is there a way to create this byte array? I am thinking if there's no way around it, I can create a byte array output stream and keep feeding it bytes, but I don't know if there

Java: Checking if a bit is 0 or 1 in a long

拈花ヽ惹草 提交于 2019-11-27 00:34:00
What method would you use to determine if the the bit that represents 2^x is a 1 or 0 ? I'd use: if ((value & (1L << x)) != 0) { // The bit was set } (You may be able to get away with fewer brackets, but I never remember the precedence of bitwise operations.) Another alternative: if (BigInteger.valueOf(value).testBit(x)) { // ... } Ande I wonder if: if (((value >>> x) & 1) != 0) { } .. is better because it doesn't matter whether value is long or not, or if its worse because it's less obvious. Tom Hawtin - tackline Jul 7 at 14:16 You can also use bool isSet = ((value>>x) & 1) != 0; EDIT: the

What is 1LL or 2LL in C and C++?

青春壹個敷衍的年華 提交于 2019-11-27 00:14:35
问题 I was looking at some of the solutions in Google Code Jam and some people used this things that I had never seen before. For example, 2LL*r+1LL What does 2LL and 1LL mean? Their includes look like this: #include <math.h> #include <algorithm> #define _USE_MATH_DEFINES or #include <cmath> 回答1: The LL makes the integer literal of type long long . So 2LL , is a 2 of type long long . Without the LL , the literal would only be of type int . This matters when you're doing stuff like this: 1 << 40

How to printf “unsigned long” in C?

 ̄綄美尐妖づ 提交于 2019-11-26 23:27:47
I can never understand how to print unsigned long datatype in C. Suppose unsigned_foo is an unsigned long , then I try: printf("%lu\n", unsigned_foo) printf("%du\n", unsigned_foo) printf("%ud\n", unsigned_foo) printf("%ll\n", unsigned_foo) printf("%ld\n", unsigned_foo) printf("%dl\n", unsigned_foo) And all of them print some kind of -123123123 number instead of unsigned long that I have. %lu is the correct format for unsigned long . Sounds like there are other issues at play here, such as memory corruption or an uninitialized variable. Perhaps show us a larger picture? NealCaffery %lu for

Convert Double to Binary representation?

喜欢而已 提交于 2019-11-26 23:11:29
I tried to convert a double to its binary representation, but using this Long.toBinaryString(Double.doubleToRawLongBits(d)) doesn't help, since I have large numbers, that Long can't store them i.e 2^900 . Long.toBinaryString(Double.doubleToRawLongBits(d)) appears to work just fine. System.out.println("0: 0b" + Long.toBinaryString(Double.doubleToRawLongBits(0D))); System.out.println("1: 0b" + Long.toBinaryString(Double.doubleToRawLongBits(1D))); System.out.println("2: 0b" + Long.toBinaryString(Double.doubleToRawLongBits(2D))); System.out.println("2^900: 0b" + Long.toBinaryString(Double