C++ How can I append an int to an int? [closed] - c++

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.

Related

can someone tell me what i did wrong [closed]

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.

Please provide me the logic to print the result using PRINTF [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 5 years ago.
Improve this question
need an programming logic to print an 4 decimal points
EX: scalar should be 0 to -5
value = 10006 , scalar = -3 then print result = 10.0060 (4 decimals)
value = 123 ,scalar = -5 then print result = 0.0012 (4 decimals)**
required value/divisor = 10 , value%divisor = 0060 (required logic after decimals )
I tried like this:
divisor = std::pow(10,std::abs(scalar));
**Result = snprintf(X,Y,"%d.%0*d",value/scalar,4,value%scalar);**
I'm not allowed to use float , setprecision() .
It does not necessarily represent the actual value , but we can format that value to print with logic like the original one (by using the logic , add ...subtract...pow etc)
std::int32_t divisor = static_cast(std::pow( 10.0F, std::abs( Scalar)) );
but int the above result modulus scalar value with 0 are not considering.
**Please provide me the logic to print the above result with scalar condition
In order to print decimals (easily), you need floating point:
printf("%10.6f", static_cast<double>(1) / 3);
If one of the arguments to division is floating point, the compiler will promote the expression to floating point.
Integral or scalar division will lose decimals.
You are always welcome to write your own division function.
Edit 1: Shifting
You don't need to use the pow function (especially since it's floating point).
Use a loop, in the loop multiply your divisor by 10 each time.
double result = (double) scalar / value;
int divisor = 10;
int i;
for (i = 0; i < NUMBER_OF_DECIMALS; ++)
{
// Isolate a digit using math.
// print the digit
divisor += 10;
}
The math part is left as an exercise for the OP.
In this homework exercise you are expected to perform your own number formatting, rather than using that of a library.
For instance, to format and output 100 as "100"
int accum = 100;
int position = 0;
while (accum > 0)
{
printf("%d", accum % 10);
accum /= 10;
position += 1;
}
For your homework assignment, you need to modify the above loop so that it puts a printf(".") in the correct place in the output number. Your answer is likely to involve multiplying accum before the loop and testing position relative to scalar

Understanding binary conversion implementation in C++ with bit operations [closed]

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.

Finding parity of number of '1's in a int variable [closed]

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.

What are the potential pitfalls with the following code [closed]

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 7 years ago.
Improve this question
Consider a simple function to divide two floats,
float floatDiv(float x, float y)
{
float z;
z = x/y;
return z;
}
All I can think of is when y==0 it has undefined behavior. Any others?
Overflow and underflow are two pitfalls, where the true result of a division cannot be held in the data type. This example shows both. In the second case of underflow, the value is too small to be represented and becomes 0.0
#include <stdio.h>
#include <float.h>
int main(){
float large, small;
large = FLT_MAX / FLT_MIN;
printf ("%f\n", (double)large);
small = FLT_MIN / FLT_MAX;
if (small == 0.0f) printf("Zero\n");
return 0;
}
Program output:
1.#INF00
Zero
One of these could happen as an intermediate step in a calculation, so might not be obvious from the range of the expected result. One practical example of that is when computing the sum of an exponential series with alternate + and - terms, such as the series for sin(x).
A pitfall with using floating point is the accuracy, since the set of values that can be represented exactly is very small compared to the values that are not represented exactly.
Given:
z = x / y;
The expression:
z * y
may not be equal to x and depends on the precision capabilities of the floating point representation.