unsigned-integer

Assign a big number to unsigned int in C

拜拜、爱过 提交于 2019-12-10 23:04:36
问题 I am trying to assign a number to unsigned int, but it results in an error. I thought as long as the number is between 0 and 2^32, it should work. Here is my code. unsigned int number = 4026658824; However, I get this error. error: constant promoted according to the 1999 ISO C standard 回答1: Type of decimal constant depends on the type in which it can be represented, per 6.4.4.1 Integer constants: The type of an integer constant is the first of the corresponding list in which its value can be

Prevent user passing negative numbers to a function accepting unsigned int

孤街醉人 提交于 2019-12-10 15:34:52
问题 So here's the code: int create_mask(unsigned b, unsigned e) { unsigned int mask=1; if(b<e || b<0 || e<0) { printf("Wrong values, starting bit can't be smaller than ending.\n"); printf("Both got to be >= 0.\n"); exit(EXIT_FAILURE); } while(b>0) { printf("%u\n", b); mask<<=1; if(b>e) mask|=1; b--; } return ~mask; /* negates mask for later purpose that is clearing corresponding bits */ } Function creates mask for some bit operations, but should take two unsigned ints b and e, both non-negative.

JNI: converting unsigned int to jint

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-10 12:29:22
问题 How do I convert an unsigned int to jint? Do I have to convert it at all, or can I just return it without any special treatment? This is basically my code right now, but I can't test it, as I haven't setup JNI locally. JNIEXPORT jint JNICALL Java_test_test(JNIEnv* env, jobject obj, jlong ptr) { MyObject* m = (MyObject*) ptr; unsigned int i = m->get(); return i; } 回答1: In the general case, jint is equivalent to int , and so can hold about half the values of unsigned int . Conversion will work

Function of type unsigned int returns negative number

大憨熊 提交于 2019-12-10 11:22:29
问题 Wow I thought I knew my C++ but this is weird This function returns an unsigned int so I thought that means I will never get a negative number returned right? The function determines how many hours ahead or behind of UTC you are. So for me I'm in Australia, Sydney so I am +10 GMT which means I am UTC = LocalTime + (-10). Therefore the GetTimeZoneInformation correctly determines I am -10. BUT my function returns an unsigned int so shouldn't it return 10 not -10? unsigned int getTimeZoneBias()

Integer promotion unsigned in c++

雨燕双飞 提交于 2019-12-09 21:59:24
问题 int main() { unsigned i = 5; int j = -10; double d = i + j; long l = i + j; int k = i + j; std::cout << d << "\n"; //4.29497e+09 std::cout << l << "\n"; //4294967291 std::cout << k << "\n"; //-5 std::cout << i + j << "\n"; //4294967291 } I believe signed int is promoted to unsigned before doing the arithmetic operators. While -10 is converted to unsigned unsigned integer underflow ( is this the correct term?? ) will occur and after addition it prints 4294967291 . Why this is not happening in

Is there a difference between unsigned and unsigned int [duplicate]

六眼飞鱼酱① 提交于 2019-12-08 11:39:38
问题 This question already has answers here : Difference between unsigned and unsigned int in C (5 answers) Closed 2 years ago . In C, is there a difference between unsigned as a type directly and unsigned int ? Update: This is a duplicate of this question, but the other question didn't answer about small subleties that can exist. Thanks @EOF for pointing out the implementation defined behavior in bitfields. 回答1: No, unsigned and unsigned int both refer to the same type. See cppreference

When casting an int64 to uint64, is the sign retained?

核能气质少年 提交于 2019-12-08 03:38:23
问题 I have an int64 variable which contains a negative number and I wish to subtract it from an uint64 variable which contains a positive number: var endTime uint64 now := time.Now().Unix() endTime = uint64(now) var interval int64 interval = -3600 endTime = endTime + uint64(interval) The above code appears to work but I wonder if I can rely on this. I am surprised, being new to Go, that after casting a negative number to uint64 that it remains negative -- I had planned to subtract the now

Function of type unsigned int returns negative number

为君一笑 提交于 2019-12-06 09:47:04
Wow I thought I knew my C++ but this is weird This function returns an unsigned int so I thought that means I will never get a negative number returned right? The function determines how many hours ahead or behind of UTC you are. So for me I'm in Australia, Sydney so I am +10 GMT which means I am UTC = LocalTime + (-10). Therefore the GetTimeZoneInformation correctly determines I am -10. BUT my function returns an unsigned int so shouldn't it return 10 not -10? unsigned int getTimeZoneBias() { TIME_ZONE_INFORMATION tzInfo; DWORD res = GetTimeZoneInformation( &tzInfo ); if ( res == TIME_ZONE_ID

Compiler Error: Invalid Conversion from int* to unsigned int* [-fpermissive]

本小妞迷上赌 提交于 2019-12-06 08:37:07
问题 I'm having the strangest issue today. I was working with an example online, and to my lack of surprise, it didn't work (they pretty much never do) . I went about fixing it myself, but I seem to be stuck on this error: Error: Invalid Conversion from int* to unsigned int* [-fpermissive] I understand this. I'm providing an int* , it wants an unsigned int* . However, I don't actually know why the int* is being generated. Here's the snippet code throwing the problem: unsigned char md_value[EVP_MAX

Java read unsigned int, store, and write it back

守給你的承諾、 提交于 2019-12-04 19:42:12
I need to read an unsigned int from a quicktime file, and write it back to another quicktime file. Currently I read the unsigned int into a Long but while writing it back I never managed to write the exact number back in 4 bytes as unsigned int. The long has the correct value that I need to write back. (eg 3289763894 or 370500) I am unable to even read the write a number smaller then Integer.MAX_VALUE (eg 2997). I am using the following methods to write the value back public void writeUInt32(long uint32,DataOutputStream stream) throws IOException { writeUInt16((int) (uint32 & 0xffff0000) >> 16