unsigned-integer

Integer promotion unsigned in c++

馋奶兔 提交于 2019-12-04 16:36:54
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 the case of int k which print -5 ? The process of doing the arithmetic operator involves a conversion

C : Convert signed to unsigned

你离开我真会死。 提交于 2019-12-04 06:34:20
问题 Actually I've (probably) a "simple" problem. So I don't know how to cast a signed integer to an unsigned integer. My code : signed int entry = 0; printf("Decimal Number : "); scanf("%d", &entry); unsigned int uEntry= (unsigned int) entry; printf("Unsigned : %d\n", uEntry); If I send the unsigned value to the console (see my last code line), I always get back an signed integer. Can you help me? Thanks a lot! Kind regards, pro 回答1: printf("Unsigned : %u\n", uEntry); // ^^ You must use the %u

C - unsigned int to unsigned char array conversion

懵懂的女人 提交于 2019-12-03 16:58:52
问题 I have an unsigned int number (2 byte) and I want to convert it to unsigned char type. From my search, I find that most people recommend to do the following: unsigned int x; ... unsigned char ch = (unsigned char)x; Is the right approach? I ask because unsigned char is 1 byte and we casted from 2 byte data to 1 byte. To prevent any data loss, I want to create an array of unsigned char[] and save the individual bytes into the array. I am stuck at the following: unsigned char ch[2]; unsigned int

Is comparing an underflowed, unsigned integer to -1 well-defined?

限于喜欢 提交于 2019-12-03 08:29:40
问题 Consider the following † : size_t r = 0; r--; const bool result = (r == -1); Does the comparison whose result initialises result have well-defined behaviour? And is its result true , as I'd expect? This Q&A was written because I was unsure of two factors in particular. They may both be identified by use of the term "crucial[ly]" in my answer. † This example is inspired by an approach for loop conditions when the counter is unsigned: for (size_t r = m.size() - 1; r != -1; r--) 回答1: size_t r =

MySQL datatype INT(11) whereas UNSIGNED INT(10)?

点点圈 提交于 2019-12-03 06:28:18
问题 in MySQL if we create a field dataType of INT and does not specify any length/values then it automatically became int(11) and if we set the attribute UNSIGNED or UNSIGNED ZEROFILL then it turns into int(10) Where does this length(1) goes? 回答1: int value can be -2147483648 these are 11 digits so the default display size is 11 unsigned int does not allow negative numbers so by default it need only display size 10 As the documentation below shows, the number of bits required to store SIGNED INT

Unsigned integers in C++ for loops

落花浮王杯 提交于 2019-12-03 06:09:59
问题 I have made some research on Stackoverflow about reverse for loops in C++ that use an unsigned integer instead of a signed one. But I still do NOT understand why there is a problem (see Unsigned int reverse iteration with for loops). Why the following code will yield a segmentation fault? #include <vector> #include <iostream> using namespace std; int main(void) { vector<double> x(10); for (unsigned int i = 9; i >= 0; i--) { cout << "i= " << i << endl; x[i] = 1.0; } cout << "x0= " << x[0] <<

Is comparing an underflowed, unsigned integer to -1 well-defined?

限于喜欢 提交于 2019-12-02 22:14:53
Consider the following † : size_t r = 0; r--; const bool result = (r == -1); Does the comparison whose result initialises result have well-defined behaviour? And is its result true , as I'd expect? This Q&A was written because I was unsure of two factors in particular. They may both be identified by use of the term "crucial[ly]" in my answer. † This example is inspired by an approach for loop conditions when the counter is unsigned: for (size_t r = m.size() - 1; r != -1; r--) size_t r = 0; r--; const bool result = (r == -1); Strictly speaking, the value of result is implementation-defined. In

How can I get the size of an std::vector as an int?

邮差的信 提交于 2019-12-02 21:51:52
I tried: #include <vector> int main () { std::vector<int> v; int size = v.size; } but got the error: cannot convert 'std::vector<int>::size' from type 'std::vector<int>::size_type (std::vector<int>::)() const noexcept' {aka 'long unsigned int (std::vector<int>::)() const noexcept'} to type 'int' Casting the expression to int like this: #include <vector> int main () { std::vector<int> v; int size = (int)v.size; } also yields an error: error: invalid use of member function 'std::vector<_Tp, _Alloc>::size_type std::vector<_Tp, _Alloc>::size() const [with _Tp = int; _Alloc = std::allocator<int>;

Unsigned integers in C++ for loops

∥☆過路亽.° 提交于 2019-12-02 19:36:18
I have made some research on Stackoverflow about reverse for loops in C++ that use an unsigned integer instead of a signed one. But I still do NOT understand why there is a problem (see Unsigned int reverse iteration with for loops ). Why the following code will yield a segmentation fault? #include <vector> #include <iostream> using namespace std; int main(void) { vector<double> x(10); for (unsigned int i = 9; i >= 0; i--) { cout << "i= " << i << endl; x[i] = 1.0; } cout << "x0= " << x[0] << endl; return 0; } I understand that the problem is when the index i will be equal to zero, because

How to load unsigned ints into SIMD

╄→尐↘猪︶ㄣ 提交于 2019-12-02 17:09:20
问题 I have a C program where I have a few arrays of unsigned ints. I'm using this declaration uint32_t . I want to use SIMD to perform some operations on the data stored in each of the arrays. This is where I'm stuck because it looks like most of the SSE and SSE2 functions only support float and double. What's the best way for me to load data of type uint32_t ? 回答1: For any integer SSE type you typically use _mm_load_si128 / _mm_loadu_si128 : uint32_t a[N]; __m128i v = _mm_loadu_si128((__m128i *