Linked Questions
14 questions linked to/from Allowing signed integer overflows in C/C++
104
votes
3
answers
54k
views
Is signed integer overflow still undefined behavior in C++?
As we know, signed integer overflow is undefined behavior. But there is something interesting in C++11 cstdint documentation:
signed integer type with width of exactly 8, 16, 32 and 64 bits ...
2
votes
3
answers
22k
views
Rotate right by n only using bitwise operators in C
I'm trying to implement a rotateRight by n function in C by only using bitwise operators.
So far, I have settled on using this.
y = x >> n
z = x << (32 - n)
g = y | z
So take for ...
6
votes
4
answers
1k
views
Is signed integer overflow undefined behaviour or implementation defined?
#include <limits.h>
int main(){
int a = UINT_MAX;
return 0;
}
I this UB or implementation defined?
Links saying its UB
https://www.gnu.org/software/autoconf/manual/autoconf-2.63/html_node/...
3
votes
9
answers
826
views
Simple Character Interpretation In C
Here is my code
#include<stdio.h>
void main()
{
char ch = 129;
printf("%d", ch);
}
I get the output as -127. What does it mean?
-1
votes
3
answers
2k
views
Cyclic Nature of char datatype [duplicate]
I had been learning C and came across topic called Cyclic Nature of Data Type in C.
It is like example
char c=125;
c=c+10;
printf("%d",c);
The output is -121.
the logic given was
125+1= 126
125+...
4
votes
2
answers
3k
views
How does int(or long long) overflow in c++ affect modulus?
Suppose I have two long longs, a and b, that I need to multiply, then get the value mod k for some large k, such that a, b, and k are all in the range of long long but not of int. For simplicity, a, b ...
6
votes
3
answers
169
views
Where is the second overflow in this piece of code
Here is the piece of code from GNU C reference manual Pg 74:
If your code uses a signed loop index, make sure that the index cannot
overflow, along with all signed expressions derived from the ...
0
votes
2
answers
917
views
C - Find Prime number
This is my prime number function and I have a problem. Why does it return 0 when I pass it 2147483647?
int ft_is_prime(int nb)
{
if (nb<2)
return (0);
else if (nb==2)
return (1);
...
-1
votes
1
answer
722
views
What happens if you exceeds the maxium value of short int(c++)?
What value will I get with this code for example,
short int num = 32766
num + 5 = ?
Maxium value for short int is 32767.
1
vote
1
answer
556
views
Can std::chrono::duration<std::chrono::nanoseconds> run into an overflow? I
I am implementing a game engine and am implementing two version of time:
Real time
Scaled time (for slow / fast motion effects)
Real time fully implemented and works, but I am having serious ...
1
vote
1
answer
158
views
While loop carries on forever (C++)
I'm still quite new to c++ and just experimenting with the language.
I have recently created a 'tictactoe' game.
I have created this simple looking function to get the users board position (from 1 ...
2
votes
1
answer
75
views
How can an integer variable show a correct value even when the value crosses its range?
I have executed this program on 32bit Windows 7 OS with Turbo C/C++ editor.
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c,d,e;
clrscr();
a = 25000;
b = ...
-3
votes
1
answer
81
views
get confused by mutlibly by 10 and then divide by 10
I am confused a little bit as when I multiply an int variable by 10 and then divide it by 10 I thought the variable value should not be changed but I get a different result am I missing something or ...
-3
votes
1
answer
114
views
When printing a large integer, the system automatically changed the number without giving error message. What can i do?
Here's the piece of code:
int catalan(int n)
{
if (n<0){
return 0;
}
if (n<=1){
return 1;
}
int total = 0;
for(int i=0;i<n;i++)
{
total += ...