Linked Questions
24 questions linked to/from Why is 0 < -0x80000000?
165
votes
2
answers
12k
views
Why does the smallest int, −2147483648, have type 'long'? [duplicate]
For a school project, I've to code the C function printf. Things are going pretty well, but there is one question I can't find a good answer to, so here I am.
printf("PRINTF(d) \t: %d\n", -2147483648)...
5
votes
2
answers
1k
views
Why is 1 not greater than -0x80000000 [duplicate]
Why is 1 not greater than -0x80000000. I know it has something to do with overflow. But can someone explain why? is 0x80000000 not a constant I think it is?
assert(1 > -0x80000000);
The assert ...
252
votes
3
answers
31k
views
(-2147483648> 0) returns true in C++?
-2147483648 is the smallest integer for integer type with 32 bits, but it seems that it will overflow in the if(...) sentence:
if (-2147483648 > 0)
std::cout << "true";
else
...
28
votes
4
answers
5k
views
How can I prevent the gcc optimizer from producing incorrect bit operations?
Consider the following program.
#include <stdio.h>
int negative(int A) {
return (A & 0x80000000) != 0;
}
int divide(int A, int B) {
printf("A = %d\n", A);
printf("negative(A) = ...
16
votes
2
answers
6k
views
Casting minimum 32-bit integer (-2147483648) to float gives positive number (2147483648.0)
I was working on an embedded project when I ran into something which I thought was strange behaviour. I managed to reproduce it on codepad (see below) to confirm, but don't have any other C compilers ...
16
votes
2
answers
4k
views
large negative integer literals
On Visual Studio 2010 the following program
#include <iostream>
using std::cout;
int main()
{
cout << -2147483646 << '\n';
cout << -2147483647 << '\n';
cout ...
10
votes
4
answers
8k
views
Why it is different between -2147483648 and (int)-2147483648
When I run the following code under Windows7 x64, compiled with GCC of MinGW, the result seems to be underflowed:
cout<<-2147483648 ; //Output: 2147483648
but when I assigned it to a integer ...
7
votes
3
answers
8k
views
Defining (1 << 31) or using 0x80000000? Result is different
#define SCALE (1 << 31)
#define fix_Q31_80(x) ( (int) ( (float)(x)*(float)0x80000000 ) )
#define fix_Q31_SC(x) ( (int) ( (float)(x)*(float)SCALE ) )
int main()
{
int fix_80 = ...
12
votes
1
answer
768
views
Why does MSVC pick a long long as the type for -2147483648?
My snippet:
auto i = -2147483648;
int j = 3;
std::swap(i, j); // Compile error about mismatched types here.
The compiler states that the literal i is a long long. Why is that? -2147483648 fits in an ...
6
votes
4
answers
6k
views
Extract upper and lower word of an unsigned 32-bit integer
To extract the upper and lower word of an unsigned 32-bit integer and store each one in separate uint16_t-variables I do as follows (nbr is an unsigned 32-bit integer):
uint16_t lower_word = (...
1
vote
2
answers
6k
views
c good programing practice using #define and hex values instead of decimal [closed]
I'm trying to improve my coding practice for embedded. The code below writes the value 0 to the memory location 0x80001000.
#define MemoryWrite(A,V) *(volatile unsigned long*)(A)=(V)
#define FLAG ...
6
votes
3
answers
697
views
-9'223'372'036'854'775'808LL is unsigned
Since C++20 two's complement representation is the only representation allowed by the standard, with the guaranteed range from -2N-1 to +2N-1-1. So for a 64-bit signed integer type the range goes from ...
0
votes
3
answers
2k
views
How to compare uint8_t array to a hex value in C?
I have predefined HEX values in my code. One of them is in the following.
#define ADDRESS1 0xD445556BD557
#define ADDRESS2 0xED612BDF113B
I also have an uint8_t array. Like
uint8_t MAC[6];
How can ...
7
votes
2
answers
365
views
How to divide by 1000000 without resulting in: undefined reference to '__udivdi3'
I have a uint64_t which I need to divide by 1000000. I am working on an embedded system and just doing the division leads to this error:
undefined reference to '__udivdi3'
I can, however, do 32-bit ...
0
votes
3
answers
1k
views
arithmetic right shift shifts in 0s when MSB is 1
As an exercise I have to write the following function:
multiply x by 2, saturating to Tmin / Tmax if overflow, using only bit-wise and bit-shift operations.
Now this is my code:
// xor MSB and 2nd ...