7 Replies - 3861 Views - Last Post: 15 August 2008 - 07:49 AM Rate Topic: -----

#1 zafar_espl  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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  Icon User is online

  • Toubabo Koomi
  • member icon

Reputation: 5246
  • View blog
  • Posts: 27,058
  • Joined: 10-May 07

Re: Increment a number without using any mathamatical Operator

Posted 14 August 2008 - 09:01 PM

Inline assembly :^:
Was This Post Helpful? 0
  • +
  • -

#3 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • 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);
Was This Post Helpful? 0
  • +
  • -

#4 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • 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

tada

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

Was This Post Helpful? 0
  • +
  • -

#5 DTR  Icon User is offline

  • New D.I.C Head

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

Re: Increment a number without using any mathamatical Operator

Posted 15 August 2008 - 05:11 AM

Ummm...

How about using logical operations instead?

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

Was This Post Helpful? 0
  • +
  • -

#6 OliveOyl3471  Icon User is offline

  • Everybody's crazy but me!
  • member icon

Reputation: 134
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

#7 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • 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 :P

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.
Was This Post Helpful? 0
  • +
  • -

#8 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3101
  • View blog
  • Posts: 19,141
  • Joined: 14-September 07

Re: Increment a number without using any mathamatical Operator

Posted 15 August 2008 - 07:49 AM

That was an enlightening thread.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1