-5

I have to add 1 to a number in C. I have to do this without arithmetic operators like '+', '++', .. so on.

I have written following line of code.

int a = 1234;
int b = 1;
printf("%d", a ^ b);

This works fine till the integer limit is reached i.e., for 32 bit it is 4294967295. But I see in many other websites that to perform the same they do AND of two numbers, followed by XOR and left shift.

Please suggest whether my approach is correct as I am novice in C.

jww
  • 97,681
  • 90
  • 411
  • 885
puneet
  • 31
  • 1
  • 2
  • 17
    Umm... what about `+` – Karol S Aug 05 '14 at 11:20
  • 2
    Bitwise XOR doesn't *add* one to a number, it *toggles* bits, in your case the least significant bit. – Some programmer dude Aug 05 '14 at 11:21
  • 1
    When `a` is `1235` the result is `1234`. Is this what you intended? Because this is not "adding 1 to a number". – Magnus Hoff Aug 05 '14 at 11:22
  • 3
    The `^` operator is a half-adder: it adds one, and ignores the carry. – Sergey Kalinichenko Aug 05 '14 at 11:25
  • As @dasblinkenlight said, you performed a half-add operation, which doesn't take carries into account. What you want is a full adder. –  Aug 05 '14 at 11:38
  • Seriously?! It's very simple-Use "a++" to increment "a" by 1 – Spikatrix Aug 05 '14 at 11:57
  • but I have to do this without arithmetic operators like '+', '++',.. so on – puneet Aug 05 '14 at 13:30
  • 2
    puneet, if you have a more specific task than "add one to a number", please specify *all* your constraints exactly. For example, when you say "'+', '++',... so on" that is not precise enough. Please specify exactly what you can and cannot use. Otherwise, we cannot know how to help you. For the question that you have asked, using `+` is the only sensible answer. – Magnus Hoff Aug 05 '14 at 20:28
  • Doing something like this seems trivial at first, but it actually isn't. Consider what happens when you perform 9999+1. For each 9 digit, you must also account for the carried digit. This process is referred to in circuit design as a ripple-carry algorithm, where you compute each sum using a full adder, accounting for the carry. You can [read more on Wikipedia](https://en.wikipedia.org/wiki/Adder_%28electronics%29). There are other adder designs listed there of course, so take your pick. Just remember some are more difficult than others to implement. I mentioned ripple-carry since it's easy. –  Aug 06 '14 at 01:20
  • @jww How else do you reliably add two numbers without the `+` operator? –  Aug 18 '14 at 03:40
  • @Chrono - I saw his comment about no arithmetic operations after I commented to you. Sorry about that. I added that to his question and then moved to re-open. His question and your comment make more sense now. – jww Aug 18 '14 at 03:46

3 Answers3

5

Your approach is not correct. You should use + to add numbers in C:

int a = 1234;
int b = 1;
printf("%d\n", a + b); // Prints 1235

You say "This works fine till the integer limit is reached" about your approach, but you are mistaken. When a is an odd number, the result of the ^ operation is a subtraction of one:

int a = 1235;
int b = 1;
printf("%d\n", a ^ b); // Prints 1234
printf("%d\n", a + b); // Prints 1236, using the correct operator

Here you can see that the approach you are currently using is not correct for odd numbers.

Magnus Hoff
  • 21,529
  • 9
  • 63
  • 82
2

In this method you are not doing exact addition. but it looks like.

   a = 00000000 00000000 00000100 11010010
   b = 00000000 00000000 00000000 00000001
 ------------------------------------------
 a^b = 00000000 00000000 00000100 11010011
 ------------------------------------------
                                         ^ this bit is toggled

For example when you try to XOR 3 with 1234, you will get output as 1233. because of XOR operation

   a = 00000000 00000000 00000100 11010010
   b = 00000000 00000000 00000000 00000011
 ------------------------------------------
 a^b = 00000000 00000000 00000100 11010001
 ------------------------------------------
                                        ^ here this bit is toggled.

When you use XOR for addition it is not meaning that you are adding. So keep this in mind and do the necessary operations!

Sathish
  • 3,740
  • 1
  • 17
  • 28
  • *"Always prefer+ for addition..."* - OP added an important comment after you posted your answer. He cannot use arithmetic operations. I suppose that means he is only allowed to use bitwise operations. – jww Aug 18 '14 at 03:45
0

^ is used to do a XOR operation, and + is used to do an "add" operation.
I highly recommed + to perform any kind of adding stuff, like the sum of two numbers, or the subtraction of a number and the negative of another number, or incrementing the value of a variable in some amount.

Besides, it is possile to increment in exactly "1" the value a variable by using the ++ unary operators: x++ is equivalent to x = x + 1.

Also, the += operator is available, which increments the variable by the quantity specified at the right: x += 1.

If one is lucky, it is possible to choose an arbitrary operator, say <OPER>, and write some expression, like x <OPER> <FREAK OPERAND> and thus to obtain the effect of "adding 1" to the variable.
But then it could be difficult to generalize this approach to more complicated situations (for exammple, the "add 2" operation).

pablo1977
  • 4,281
  • 1
  • 15
  • 41