Create an overflow at a given value [closed] - c++

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
If a unsigned byte overflows it goes from 255 to 0 and vica versa -1 gives 255.
Would it be possible to have it overflow at for example 200?
Without using if statements.

Overflow is fairly simple:
unsigned int a = 150, b = 150;
a += b; // do an operation
a %= 200; // wrap it
However, with underflow, it's a bit harder (see orlp's answer for this).
To make it less error prone if you use this variable several times, in C++ with operator overloading, you can make a class that simulates an integer type which wraps after every operation with operator overloading.

The modulo operator does what you want, with some trickery for negative values:
int wrap(int x, int n) {
return x < 0 ? ((x % n) + n) % n : x % n;
}
// wrap(205, 200) == 5
// wrap(-1, 200) == 199

Unless your willing to learn assembly, such an action would be impossible for several reasons.
All the types like char, short, int, etc. are builtin and predefined by the parser.
200 isnt a power of two; computer represent numbers in binary.
Note: The above is only true if you want implicit overflow; modulas lets you do explicit overflow.

Related

Why doesn't this code use the C++ power function directly? Can someone help me with the power function in this code? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
This problem is to output a single integer, the number of possible combinations calculated:
int power(int a, int n)
{
if (n == 0)
return 1;
// else
if (n % 2 == 0) {
int temp = power(a, n / 2);
return temp * temp;
}
// else
return a * power(a, n - 1);
}
This function uses a technique called exponentation by squaring.
It's a particularly efficient way of evaluating the power for integral type arguments. The standard C function uses floating point arguments, and the C standard doesn't require an exact result even if the floating point arguments represent whole numbers.
In C++ though you can probably rely on one of the overloads of std::pow that takes integral type arguments, and cast the result, subject to your making the necessary size checks. But again even the C++ standard does not require that the best possible result is returned (cf. std::sqrt under IEEE754), although one could reasonably regard a std::pow function that does not return the correct result for integral arguments to be defective.

What is the real powerful use of shift operators in C ? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
One purpose of left shift operator is multiply the left Operand with 2 and right shift for integer Division. Both have also constraints like undefined behaviours Link etc. I do not understand what will be the real use. I am even not confident to use them for Division or multiplication.
Only beginners would ever use a shift for division or multiplication (including people who are writing software for decades and are still beginners).
Shift operations are for shifting bits. When you want to shift bits you use them. If you don't know what shifting bits means, you don't want to use them.
They're bitwise operators, so they're used for bitwise operations. Here's a trivial example that shows some bitwise operations that call for bit shifting.
struct color {
unsigned char r;
unsigned char g;
unsigned char b;
};
void setColor(struct color*,int);
int getColor(struct color*);
int main() {
struct color myColor;
int color;
setColor(&myColor,0x00ff00);
color = getColor(&myColor);
return 0;
}
void setColor(struct color* color,int rgb) {
color->r = (rgb>>16)&0xff;
color->g = (rgb>>8)&0xff;
color->b = rgb&0xff;
}
int getColor(struct color* color) {
return color->r<<16|color->g<<8|color->b;
}
One real-world use case is in manipulating bitmasks. For example, if I have a bitmask x and I want to set the 7th bit from the right (where I start counting at zero), I could do the following.
x = x | (1 << 7);

difference between literal types and variable types in c++ [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I was studying C++ via cplusplus.com and came across something like 75u, which seems to describe an unsigned constant.
What has got me confused is: what's the point of declaring a constant to be unsigned when there is already a provision to declare the variable to which 75 will be assigned as unsigned?
Simpler said:
Why would you specifically add a u to a number when assigning it to (for example) an unsigned int?
What's the difference between
unsigned int i = 75;
and
unsigned int i = 75u;
That's because the type of the variable (in an assignment) on the left hand side of the = has nothing to do with how an expression is evaluated (the right hand side).
This seems to surprise many new programmers, but it's still true.
Something like this:
const float two_thirds = 2 / 3; /* Bad code! */
does not assign 0.6666667 to two_thirds; since both 2 and 3 are int literals, the expression is evaluated using integer math.
You need:
const float two_thirds = 2.f / 3;
to force the expression to float. Similar reasoning applies to the use of unsigned, since it has larger range than signed variables.

What type should I use if I want to assign 500*(1.00013358^3) to a variable [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
Trying to write a small program to calculate increased by factor numbers using c++.
I'm trying to assign increased by factor to variable to calculate some numbers.
I always get invalid operands of type double and int.
What type should I use if I want to assign 500 * (1.00013358^3) to a variable?
Here's some example that I tried without luck:
int i=500 * (1.00013358^3);
then:
float i =500 * (1.00013358^3);
but I always get invalid operands of type double and int
As ^ isn't the power operator I'm assuming you want (it's in fact the bitwise xor operator), you might want to try something like this:
double x = 1.00013358;
double i = 500*x*x*x; // i == 500 * std::pow(1.00013358, 3)
I assume that your expectation is that ^ is some kind of power operator. But it is not, it is a bitwise exclusive or (XOR) and can only be applied to integral types.
You may want to have a look at std::pow, from the cmath header:
double i = 500 * std::pow(1.00013358, 3);
Welcome to C++, ^ is for bitwise XOR. If you want to do power-thing, use pow(). And both float and double will work OK for your case.
float i = 500 * powf(1.00013358, 3);

Calculate the log base n with Shift left or Shift right [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a little problem.
Who knows how we can calculate the log
base n with Shift_L or Shift_R?
for example: for n=2 we had this solution:
int log(int n){
int res = 0;
while((n>>=1))
res++;
return res;
}
You don't seem to want the logarithm for a base b, but the largest integer n so that n <= log_b(x). If that's the case, the following function should serve your needs:
int intlog(double base, double x) {
return (int)(log(x) / log(base));
}
well this is rather a math problem instead of an actuall programming problem, if i understand your problem correctly:
log_2 (x) = log_a (x) / log_a (2) where a can be any base.
Therefore you could use the math.h's function log(double)
double res = log(x)/log(2);