Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
It keeps saying
Cannot implicitly convert type 'long' to 'int'. An explicit conversion exists (are you missing a cast?)
int lVar10 = 0x7a69;
int uVar12 = lVar10 * 0x1a3 + 0x181d;
lVar10 = uVar12 + ((uVar12 & 0xffffffff) / 0x7262) * -0x7262;
In short, the numbers you are trying to operate are bigger than the highest possible int value, specifically at the row
lVar10 = uVar12 + ((uVar12 & 0xffffffff) / 0x7262) * -0x7262;
In decimal that number is 2147483647 = 2^31 - 1. Since you are using hexa to represent numbers (with the prefix 0x), the 0xffffffff alone is already bigger than the highest integer.
So you could fix this by declaring lVar10 and lVar12 as long.
You should cast the variables to a long. Adding
(long) after the = in the third line should solve it.
Edit:
Also, you could just declare the variable lVar10 and uVar12 as long in the first place.
You are trying to store a non-integer into an integer variable.
The variable named lVar10 can be assigned integers. For example, values like -3, -2, -1, 0, 1, 2, 3.
Related
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 2 years ago.
Improve this question
Is there a way to convert a double into an integer without risking any undesired errors in the process? I read in Programming - Principles and Practice Using C++ (a book written by the creator of c++) that doubles cannot be turned into integers, but I've put it to the test, and it converts properly about 80% of the time. What's the best way to do this with no risk at all, if it's even possible?
So for example, this converts properly.
double bruh = 10.0;
int a = bruh;
cout << bruh << "\n";
But this doesn't.
double bruh = 10.9;
int a = bruh;
cout << bruh << "\n";
In short, it doesn't round automatically so I think that's what constitutes it as "unsafe".
It it not possible to convert all doubles to integers with no risk of losing data.
First, if the double contains a fractional part (42.9), that fractional part will be lost.
Second, doubles can hold a much larger range of values than most integers, something around 1.7e308, so when you get into the larger values you simply won't be able to store them into an integer.
way to convert a double into an integer without risking any undesired errors
in short, it doesn't round automatically so I think that's what constitutes it as "unsafe"
To convert to an integer value:
x = round(x);
To convert to an integer type:
Start with a round function like long lround(double x);. It "Returns the integer value that is nearest in value to x, with halfway cases rounded away from zero."
If the round result is outside the long range, problems occur and code may want to test for that first.
// Carefully form a double the is 1 more than LONG_MAX
#define LONG_MAXP1 ((LONG_MAX/2 + 1)*2.0)
long val = 0;
if (x - LONG_MAXP1 < -0.5 && x - LONG_MIN > -0.5) {
val = lround(x);
} else {
Handle_error();
}
Detail: in order to test if a double is in range to round to a long, it is important to test the endpoints carefully. The mathematical valid range is (LONG_MIN-0.5 ... LONG_MAX + 0.5), yet those endpoints may not be exactly representable as a double. Instead code uses nearby LONG_MIN and LONG_MAXP1 whose magnitudes are powers of 2 and easy to represent exactly as a double.
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 5 years ago.
Improve this question
I have a problem in c++, I have this code to do a division:
long a = 33732193487;
int b = 1000000;
double c = (double)a/(double)b;
Well, I would like to store the complete result in the variable c, I mean:
c = 33732.193487;
for later, for example, use this value to make a sum or substrac. But I only get to store the result with one decimal.
Is there any option to store the 6 decimals with c++?
Thank you in advance! Sorry if my question is too obvious.
Let me guess (a complete program would have been better): you're seeing six decimal places when you write the value to standard out:
std::cout << c << '\n';
Those six decimal places are the default for output; they have nothing to do with the precision of the value of c, which is determined by the type of c. For double you typically have about fifteen digits. To see those digits in your output statement, use std::setprecision:
std::cout << std::setprecision(15) << c << '\n';
But beware: you can use values greater than 15 in the call to setprecision and the stream inserter will happily generate more digits, with nonsensical values.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I am interested in understanding the implementation of converting decimal to binary. Could somebody clarify the purpose of using left-shift and Right-shift in the following code?
void static inline unsignedToBinary(unsigned x, char*& bin)
{
bin = (char*) malloc(33);
int p = 0;
for (unsigned i = (1 << 31); i > 0; i >>= 1)
bin[p++] = ((x&i) == i) ? '1' : '0';
bin[p] = '\0';
}
This is a straightforward implementation of binary conversion that uses bit operations.
Variable i represents the mask - an int containing 2k value, where k is the position of the bit.
The initial value is 231, produced with left-shifting 1 by 31.
for loop uses >>= 1 to right-shift the mask until 1 gets shifted out of it, making i == 0.
At each iteration x&i is compared to i. The comparison succeeds when x contains 1 in the position where i has its 1; it fails otherwise.
Note: Although using malloc in C++ is certainly allowed, it is not ideal. If you want to stay with C strings, use new char[33] instead. A more C++-like approach would be using std::string.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
Given an int variable, I would like to check if the number of '1' in its binary representation is even or odd. It can be made with xor operations like
int n;
int s = 0;
for(;n;n>>=1)
s ^= (n&1);
There's some better way to do so in C++?
Note: I'm not asking for the number of '1's, but for its parity, so I thought there could be some better code than mine.
uint32_t v = somevalue;
v ^= v >> 1;
v ^= v >> 2;
v = (v & 0x11111111U) * 0x11111111U;
bool parity = (v >> 28) & 1;
From https://graphics.stanford.edu/~seander/bithacks.html
It has a 64bit variant too.
For clarification, with "parity" I don't mean if the number is even or odd mathematically, but if the count of 1 bits in it's binary representation is even or odd; like described in https://en.wikipedia.org/wiki/Parity_bit. With the maths meaning, the code in the question makes no sense, so I assumed OP means the same. The statement
I'm not asking for the number of '1's, but for its parity
then means that he/she just wants to know if the 1 count is even or odd,
but not the exact number of 1's.
If you are really after speed, you can tabulate the number of bits (or just its parity) for all byte values 0..255. Then mapping a union on the variable or using shifts/masks, accumulate for the four bytes.
Even faster and more paranoid, tabulate for all short values 0..65535.
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 9 years ago.
Improve this question
How can I append an int to an int:
x = 23;
y = 54;
result = 2354;
I hope you help me.
Here are two general approaches:
"Shift" the left integer over by multiplying by the appropriate power of 10 and then add the right integer. In the example code that is x * 100 + y (or x * pow(10,2) + y) as shown in a comment.
The value to shift can be derived from the ceiling of the log10 of the right number. Using the math above, this could be expanded more generally as x * pow(10, ceil(log10(y))) + y.
Convert the integers to strings, concatenate the strings, and convert the resulting string back to an integer.
Take the inputs as a string
concatenate the strings. or you can use append to add the strings.