This question already has answers here:
How to concatenate two integers in C
(10 answers)
Closed 5 years ago.
Currently I am having a speed bump with adding two different integers. For example
int i = 32;
int j = 50;
/* Add i and j together into 3250 */
What I thought was changing the integers into strings and add them together but that takes too much effort. Is there any other way?
The solution in the decimal system is:
int result = 100* i + j;
In case this should be generic you'll need the following algorithm:
int shift = 10;
while(j >= shift) {
pow *= 10;
}
int result = i * pow + j;
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I have two large numbers stored in an array(p->numbers[50] and q->numbers[50]), and printed out in hexadecimal
1319df046
111111111
When added together, I am returned with, in hexadecimal,
242af'11'257
However, apparently my answer "should" be
242af0157
There is a discrepancy when adding the f and 1 together, equaling 17, but printing 11 (as 17 is 11 in hexadecimal). I'm not sure why my output should be a 0 instead of 11
int sum = 0;
int carry = 0;
for(i = 9; i >= 0; i--)
{
sum = p->numbers[i] + q->numbers[i];
sum = sum + carry;
answer[i] = sum;
carry = sum / 10;
printf("%x", answer[i]);
}
I reproduced your results by defining the arrays of digits as follows:
int p[] = {0x6,0x4,0x0,0xf,0xd,0x9,0x1,0x3,0x1,0x0};
int q[] = {0x1,0x1,0x1,0x1,0x1,0x1,0x1,0x1,0x1,0x0};
This is not storing the number as decimal, but as hexadecimal digits.
With that in mind, there are three problems here:
First, the way you're calculating the carry is incorrect. Because the digits are hexadecimal and not decimal, the carry should be sum / 16 instead of sum / 10.
Second, when there is a carry involved, you're not removing the high digit of the sum. In one place, you have 0xf + 0x1 + 0x1 = 0x11 and both characters are being printed. You need to set the digit as answer[i] = sum % 16;
Third, you're adding the digits from largest to smallest. You need to add them from smallest to largest in one loop, then print the digits from largest to smallest in a separate loop.
With those fixes in place, your code should look like this:
for(i = 0; i < 10; i++)
{
sum = p[i] + q[i];
sum = sum + carry;
answer[i] = sum % 16;
carry = sum / 16;
}
for(i=9; i>=0; i--) {
printf("%x", answer[i]);
}
This question already has answers here:
Add two integers using only bitwise operators?
(8 answers)
How to add two numbers without using ++ or + or another arithmetic operator
(21 answers)
Closed 6 years ago.
I was asked this question in an interview:
How to add two variables without using a '+' operator?
The interviewer asked me and I could not answer, even though I am a good C programmer!
Using bitwise operators you can add two numbers. Try below:
int Sum(int a, int b)
{
// Iterate till there is no carry
while (b != 0)
{
// now carry contains common set bits of a and b
int carry = a & b;
// Sum of bits of a and b where at least one of the bits is not set
a = a ^ b;
// Carry is shifted by one so that adding it to a gives the required sum
b = carry << 1;
}
return a;
}
Using Increment and decrement operators you can add two numbers.
The other way may be like:
int Sum(int a, int b)
{
// Iterate till there b becomes zero
while (b--)
{
a++;
}
return a;
}
This question already has answers here:
Why does rand() always return the same value? [duplicate]
(2 answers)
Closed 7 years ago.
I have a big problem , I do not understand.
I need to generate random numbers from the interval.
I am using code:
unsigned int nahodnyCisloZIntervalu(unsigned int min, unsigned int max) {
int r;
const unsigned int range = 1 + max - min;
const unsigned int buckets = RAND_MAX / range;
const unsigned int limit = buckets * range;
do {
r = rand();
} while (r >= limit);
return min + (r / buckets);
}
But every time you start the program generates the same numbers!
How to generate truly random numbers usnig C?
Use the standard rand function. It is normal that rand returns the same sequence each time.
Use the srand function at the beginning of your program in order to initialize the random number generator with a "seed", using for example the current time as seed.
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
What does rand() do in C? I don't use C++, just C.
Visual Studio 2012 tells me that its return type is int __cdecl
And it is part of stdlib.h
It does not take any parameters.
How can I set the range in which it generates (pseudo)random numbers?
Your answers are greatly appreciated
Now that there's an actual question: You can't. The range is fixed, and it's defined by the constant RAND_MAX if you need to know it (it's [0 .. RAND_MAX])
If you want a different range, you have to arrange that yourself, typically using the modulo operator % and optionally an offset ... for random numbers between 5 and 9 use
int foo = rand() % 5 + 5;
I use a helper function like this in the game i recently wrote:
int
randomNum(int min, int max)
{
static int seeded = 0;
if (!seeded)
{
seeded = 1;
srand((unsigned int)time(0));
}
return (rand() % (max-min+1)) + min;
}
Depends on the compiler. This wiki article contains a list of them: linear congruential generator
If you need to extend the range of rand(), call it multiple times and merge the outputs:
unsigned int r;
/* ... */
r = ((rand()>>4) & 0xff)<< 0;
r += ((rand()>>4) & 0xff)<< 8;
r += ((rand()>>4) & 0xff)<<16;
r += ((rand()>>4) & 0xff)<<24;
Example program to show how rand works by comparing the returned value. This works with Microsoft compiles (no mismatch).
#include <stdio.h>
#include <stdlib.h>
int main(int argc, const char* argv[])
{
unsigned int seed = 1;
unsigned int rand1, rand2;
unsigned int i;
for(i = 0; i < 20; i++){
seed = seed*214013 + 2531011;
rand1 = (seed >> 16) & 0x7fffu;
rand2 = rand();
if(rand1 != rand2)
printf("mismatch %d %d\n", rand1, rand2);
}
return(0);
}
Since only 15 bits of the seed are returned by this version of rand(), then RAND_MAX would be 32767 or hex 0x7fff. As noted in the wiki article, the period is 2^32, this means that seed will cycle through all 4,294,967,296 possible 32 bit values, never repeating until the 4,294,967,296 call to rand(), where seed will have cycled back to 1.
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 want to append any static single digit in given number using bitwise operation.
Let the static number is 1 ,
If the number is
2 => 12
31 => 131
24 => 124
11 => 111
Is it possible to do..?
Here why I am strict with bitwise means , I want to maintain as integer values.
If your dbms supports basic math functions (Oracle for instance), you can use:
SELECT NUMBER + Power(10, Floor(Log(10, NUMBER)) + 1)
FROM TABLE;
If not you could get away with a string trick like:
SELECT TO_NUMBER('1' || TO_CHAR(NUMBER))
FROM TABLE;
(using Oracle)
Bitwise operators won't work that well on decimal numbers since powers of two and powers of ten don't mesh well (I have no idea what the database tag is for here, this seems to be totally unrelated).
If you want a function to add a 1 to the left of an arbitrary number as you seem to indicate, you can use the following algorithm:
def prefixWithOne (n):
if n == 0:
return 10
addNum = 1
testNum = n
while testNum > 0:
testNum = int (testNum / 10)
addNum = addNum * 10
return addNum + n
For example, the following C code will do that:
unsigned int prefixWithOne (unsigned int n) {
unsigned int testNum = n;
unsigned int addNum = 1;
if (n == 0) return 10;
while (testNum > 0) {
testNum /= 10;
addNum *= 10;
}
return addNum + n;
}
(with the usual caveats about watching out for overflow).