unsigned

Difference between size_t and unsigned int?

前提是你 提交于 2019-11-27 10:25:25
I am so confused about size_t . I have searched on the internet and everywhere mentioned that size_t is an unsigned type so, it can represent only non-negative values. My first question is: if it is used to represent only non-negative values, why don't we use unsigned int instead of size_t ? My second question is: are size_t and unsigned int interchangeable or not? If not, then why? And can anyone give me a good example of size_t and briefly its workings? if it is use to represent non negative value so why we not using unsigned int instead of size_t Because unsigned int is not the only

Does cast between signed and unsigned int maintain exact bit pattern of variable in memory?

爷,独闯天下 提交于 2019-11-27 10:20:21
问题 I want to pass a 32-bit signed integer x through a socket. In order that the receiver knows which byte order to expect, I am calling htonl(x) before sending. htonl expects a uint32_t though and I want to be sure of what happens when I cast my int32_t to a uint32_t . int32_t x = something; uint32_t u = (uint32_t) x; Is it always the case that the bytes in x and u each will be exactly the same? What about casting back: uint32_t u = something; int32_t x = (int32_t) u; I realise that negative

What is the difference between “int” and “uint” / “long” and “ulong”?

谁说胖子不能爱 提交于 2019-11-27 09:27:03
问题 I know about int and long (32-bit and 64-bit numbers), but what are uint and ulong ? 回答1: The primitive data types prefixed with "u" are unsigned versions with the same bit sizes. Effectively, this means they cannot store negative numbers, but on the other hand they can store positive numbers twice as large as their signed counterparts. The signed counterparts do not have "u" prefixed. The limits for int (32 bit) are: int: –2147483648 to 2147483647 uint: 0 to 4294967295 And for long (64 bit):

Strange behavior in C when calculating sum of digits with leading zeroes

老子叫甜甜 提交于 2019-11-27 08:24:27
问题 I just wanted to write a minimalistic program in C to calculate the sum of digits of some natural number (the sum of digits is defined as follows: sumOfDigits(123) = 6, sumOfDigits(0) = 0, sumOfDigits(32013) = 9, and so on). So far, everything is ok with the following code snippet. For example, for 5100 it delivers 6, correctly. But, why is 14 delivered for 05100 (remember the leading 0)? What's going on here? I had a look at the binary representation of the numbers, but that didn't give any

What happens when I assign a negative value to an unsigned int? [duplicate]

五迷三道 提交于 2019-11-27 07:53:52
Possible Duplicate: signed to unsigned conversion in C - is it always safe? Let's say I declare a variable of type unsigned int : unsigned int x = -1; Now -1 in two's complement (assuming 32 bit machine) is 0xFFFFFFFF. Now when I assigned this value to x, did the value 0x7FFFFFFF get assigned to x? If it were so, then printf ("%d",x); would have printed the decimal equivalent of 0x7FFFFFFF, right? But, clearly this isn't happening, as the value that gets printed is -1. What am I missing here? Edit: I know that we can use the %u format specifier to print unsigned values. But that doesn't help

How to convert signed to unsigned integer in python

限于喜欢 提交于 2019-11-27 06:47:32
Let's say I have this number i = -6884376 . How do I refer to it as to an unsigned variable? Something like (unsigned long)i in C. Assuming : You have 2's-complement representations in mind; and, By (unsigned long) you mean unsigned 32-bit integer, then you just need to add 2**32 (or 1 << 32) to the negative value. For example, apply this to -1: >>> -1 -1 >>> _ + 2**32 4294967295L >>> bin(_) '0b11111111111111111111111111111111' Assumption #1 means you want -1 to be viewed as a solid string of 1 bits, and assumption #2 means you want 32 of them. Nobody but you can say what your hidden

How to get the signed integer value of a long in python?

断了今生、忘了曾经 提交于 2019-11-27 05:43:21
问题 If lv stores a long value, and the machine is 32 bits, the following code: iv = int(lv & 0xffffffff) results an iv of type long, instead of the machine's int. How can I get the (signed) int value in this case? 回答1: import ctypes number = lv & 0xFFFFFFFF signed_number = ctypes.c_long(number).value 回答2: You're working in a high-level scripting language; by nature, the native data types of the system you're running on aren't visible. You can't cast to a native signed int with code like this. If

Why is −1 > sizeof(int)?

∥☆過路亽.° 提交于 2019-11-27 05:34:05
Consider the following code: template<bool> class StaticAssert; template<> class StaticAssert<true> {}; StaticAssert< (-1 < sizeof(int)) > xyz1; // Compile error StaticAssert< (-1 > sizeof(int)) > xyz2; // OK Why is -1 > sizeof(int) true? Is it true that -1 is promoted to unsigned(-1) and then unsigned(-1) > sizeof(int) . Is it true that -1 > sizeof(int) is equivalent to -1 > size_t(4) if sizeof(int) is 4. If this is so why -1 > size_t(4) is false? Is this C++ standard comformant? The following is how standard (ISO 14882) explains abort -1 > sizeof(int) Relational operator `>' is defined in 5

Negative numbers are stored as 2's complement in memory, how does the CPU know if it's negative or positive?

大城市里の小女人 提交于 2019-11-27 05:33:28
问题 -1 can be represented in 4 bit binary as (2's complement) 1111 15 is also represented as 1111. So, how does CPU differentiate between 15 and -1 when it gets values from memory? 回答1: The CPU doesn't care whether a byte holds -1 or 15 when it moves it from one place to another. There's no such thing as a "signed move" (to a location of the same size - there is a signed move for larger or smaller destinations). The CPU only cares about the representation when it does arithmetic on the byte. The

What is the difference between signed and unsigned variables?

一个人想着一个人 提交于 2019-11-27 05:06:59
问题 I have seen these mentioned in the context of C and C++, but what is the difference between signed and unsigned variables? 回答1: Signed variables , such as signed integers will allow you to represent numbers both in the positive and negative ranges . Unsigned variables , such as unsigned integers, will only allow you to represent numbers in the positive . Unsigned and signed variables of the same type (such as int and byte ) both have the same range (range of 65,536 and 256 numbers,