unsigned-integer

Why are unsigned integers error prone?

那年仲夏 提交于 2019-11-27 11:52:41
问题 I was looking at this video. Bjarne Stroustrup says that unsigned ints are error prone and lead to bugs. So, you should only use them when you really need them. I've also read in one of the question on Stack Overflow (but I don't remember which one) that using unsigned ints can lead to security bugs. How do they lead to security bugs? Can someone clearly explain it by giving an suitable example? 回答1: One possible aspect is that unsigned integers can lead to somewhat hard-to-spot problems in

What is the difference between signed and unsigned int

自作多情 提交于 2019-11-27 10:43:29
What is the difference between signed and unsigned int? Bill Evans at Mariposa As you are probably aware, int s are stored internally in binary. Typically an int contains 32 bits, but in some environments might contain 16 or 64 bits (or even a different number, usually but not necessarily a power of two). But for this example, let's look at 4-bit integers. Tiny, but useful for illustration purposes. Since there are four bits in such an integer, it can assume one of 16 values; 16 is two to the fourth power, or 2 times 2 times 2 times 2. What are those values? The answer depends on whether this

Using MySQL's TIMESTAMP vs storing timestamps directly

两盒软妹~` 提交于 2019-11-27 06:27:40
I'm in a dilemma about saving date and time values in MySQL's TIMESTAMP format vs in a custom UNSIGNED INT format. The main considerations here are speed of retrieval, appropriate range calculations in PHP and occasional formatting into human readable values. The storage space required for each type and their ranges: DATETIME 8 bytes '1000-01-01 00:00:00' to '9999-12-31 23:59:59' TIMESTAMP 4 bytes '1970-01-01 00:00:01' UTC to '2038-01-19 03:14:07' UTC UNSIGNED INT 4 bytes (Maximum Value 4294967295) I dont need the range of DATETIME at all. I'm torn between TIMESTAMP and UNSIGNED INT. Arguments

How to convert an unsigned int to a float?

自闭症网瘾萝莉.ら 提交于 2019-11-27 04:54:02
问题 I need to build a function that returns the bit-level equivalent of (float)x without using any floating data types, operations or constants. I think I have it, but when I run the test file, it returns that there's an infinite loop. Any debugging help would be appreciated. I'm allowed to use any integer/unsigned operations including ||, &&, if, while. Also, I can only use 30 operations unsigned float_i2f(int x) { printf("\n%i", x); if (!x) {return x;} int mask1 = (x >> 31); int mask2 = (1 <<

C++ Implicit Conversion (Signed + Unsigned)

老子叫甜甜 提交于 2019-11-27 01:10:27
问题 I understand that, regarding implicit conversions, if we have an unsigned type operand and a signed type operand, and the type of the unsigned operand is the same as (or larger) than the type of the signed operand, the signed operand will be converted to unsigned. So: unsigned int u = 10; signed int s = -8; std::cout << s + u << std::endl; //prints 2 because it will convert `s` to `unsigned int`, now `s` has the value //4294967288, then it will add `u` to it, which is an out-of-range value,

Cython: (Why / When) Is it preferable to use Py_ssize_t for indexing?

◇◆丶佛笑我妖孽 提交于 2019-11-27 00:42:26
问题 This is a follow-up to this question. (Why / When) Is it preferable to use Py_ssize_t for indexing? In the docs I just found # Purists could use "Py_ssize_t" which is the proper Python type for # array indices. -> Does that mean always when indexing NumPy/Cython - array(s)/-views one should use Py_ssize_t ? -> Is Py_ssize_t e. g. an unsigned int so that I can't used @cython.boundscheck(False) 回答1: Py_ssize_t is signed. See PEP 353, where it says "A new type Py_ssize_t is introduced, which has

Overflowing of Unsigned Int

你离开我真会死。 提交于 2019-11-26 22:44:55
What will the unsigned int contain when I overflow it? To be specific, I want to do a multiplication with two unsigned int s: what will be in the unsigned int after the multiplication is finished? unsigned int someint = 253473829*13482018273; unsigned numbers can't overflow, but instead wrap around using the properties of modulo. For instance, when unsigned int is 32 bits, the result would be: (a * b) mod 2^32 . As CharlesBailey pointed out, 253473829*13482018273 may use signed multiplication before being converted, and so you should be explicit about unsigned before the multiplication:

What is going on with bitwise operators and integer promotion?

不羁岁月 提交于 2019-11-26 22:01:20
问题 I have a simple program. Notice that I use an unsigned fixed-width integer 1 byte in size. #include <cstdint> #include <iostream> #include <limits> int main() { uint8_t x = 12; std::cout << (x << 1) << '\n'; std::cout << ~x; std::cin.clear(); std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); std::cin.get(); return 0; } My output is the following. 24 -13 I tested larger numbers and operator << always gives me positive numbers, while operator ~ always gives me negative numbers

Convert unsigned int to signed int C

三世轮回 提交于 2019-11-26 20:53:12
问题 I am trying to convert 65529 from an unsigned int to a signed int . I tried doing a cast like this: unsigned int x = 65529; int y = (int) x; But y is still returning 65529 when it should return -7. Why is that? 回答1: It seems like you are expecting int and unsigned int to be a 16-bit integer. That's apparently not the case. Most likely, it's a 32-bit integer - which is large enough to avoid the wrap-around that you're expecting. Note that there is no fully C-compliant way to do this because

What is the difference between signed and unsigned int

。_饼干妹妹 提交于 2019-11-26 15:16:39
问题 What is the difference between signed and unsigned int? 回答1: As you are probably aware, int s are stored internally in binary. Typically an int contains 32 bits, but in some environments might contain 16 or 64 bits (or even a different number, usually but not necessarily a power of two). But for this example, let's look at 4-bit integers. Tiny, but useful for illustration purposes. Since there are four bits in such an integer, it can assume one of 16 values; 16 is two to the fourth power, or