# Increment a number without using any mathamatical Operator

Page 1 of 1

## 7 Replies - 4583 Views - Last Post: 15 August 2008 - 07:49 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=60696&amp;s=f6b977b360a3aef64bb7eec3a7109b67&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 zafar_espl

Reputation: 0
• Posts: 2
• Joined: 14-August 08

# Increment a number without using any mathamatical Operator

Posted 14 August 2008 - 09:00 PM

Hi,

Spme one asked me how to increment a number without usiing any + sign. please tell me how can we do that.

Thank you
Is This A Good Question/Topic? 0

## Replies To: Increment a number without using any mathamatical Operator

### #2 no2pencil

• Professor Snuggly Pants

Reputation: 5963
• Posts: 29,131
• Joined: 10-May 07

## Re: Increment a number without using any mathamatical Operator

Posted 14 August 2008 - 09:01 PM

Inline assembly

### #3 NickDMax

Reputation: 2254
• Posts: 9,245
• Joined: 18-February 07

## Re: Increment a number without using any mathamatical Operator

Posted 14 August 2008 - 09:06 PM

a = -(-a - 1);

### #4 NickDMax

Reputation: 2254
• Posts: 9,245
• Joined: 18-February 07

## Re: Increment a number without using any mathamatical Operator

Posted 14 August 2008 - 09:48 PM

Another version of the above: number = -(~number);

This works because the negation operation takes the two's complement:

-a = ~a + 1

SO:

number = -(~number) == ~(~number) + 1 == number + 1

of course this is till using mathematical operators... still trying to think if you can do this without ANY mathematical operation, but I really don't think so.

BTW: This works for integral numeric types (char, short, int, long, etc) but not for floating point types (float, double, etc).

This post has been edited by NickDMax: 14 August 2008 - 11:04 PM

### #5 DTR

Reputation: 2
• Posts: 33
• Joined: 07-August 08

## Re: Increment a number without using any mathamatical Operator

Posted 15 August 2008 - 05:11 AM

Ummm...

2 Byte integer holding a value 1 would look like: 0000 0000 0000 0001 in the memory.

Bitshift 1 to left to get 2: 0000 0000 0000 0010

You could increase it from here on with bitmasks and some creative use of bit operators( ~, &, ^, |)

This post has been edited by DTR: 15 August 2008 - 05:13 AM

### #6 OliveOyl3471

• Everybody's crazy but me!

Reputation: 135
• Posts: 6,581
• Joined: 11-July 07

## Re: Increment a number without using any mathamatical Operator

Posted 15 August 2008 - 05:59 AM

That's like trying to mow the lawn with a weedwhacker when you've got a perfectly good mower sitting right there.
Is there ever a reason that you would need to do this?

This post has been edited by OliveOyl3471: 15 August 2008 - 06:02 AM

### #7 NickDMax

Reputation: 2254
• Posts: 9,245
• Joined: 18-February 07

## Re: Increment a number without using any mathamatical Operator

Posted 15 August 2008 - 07:06 AM

@DTR -- a bit shift is a multiplication by 2. So while it looks like it increments it only works for the number 1, after that things go down hill. Now you could implement the binary addition algorithm. Just seems like a lot of work.

@OliveOyl3471 -- what if your + is broken? Got bumped so that the it looks like a half hearted mix between the division symbol and the percent sign. Do you have any idea how many times I drop my laptop! Something like this could happen to me any day now

Actually I probably did someone's homework for them. These kinds of questions are common since they force you to think a little outside of the box. They test or understanding of what is going on. -- or in this case they test your ability to use the internet as a resource in solving problems.

I am a sucker for a good: "How do you do _______ given these _________ restrictions that stop you from any obvious solution?" type of questions. We had a nice discussion on how to get around the need for an if-statement a while back.

### #8 KYA

• yay verily

Reputation: 3155
• Posts: 19,200
• Joined: 14-September 07

## Re: Increment a number without using any mathamatical Operator

Posted 15 August 2008 - 07:49 AM