Hi,
Spme one asked me how to increment a number without usiing any + sign. please tell me how can we do that.
Thank you
7 Replies  4347 Views  Last Post: 15 August 2008  07:49 AM
#1
Increment a number without using any mathamatical Operator
Posted 14 August 2008  09:00 PM
Replies To: Increment a number without using any mathamatical Operator
#2
Re: Increment a number without using any mathamatical Operator
Posted 14 August 2008  09:01 PM
Inline assembly
#3
Re: Increment a number without using any mathamatical Operator
Posted 14 August 2008  09:06 PM
a = (a  1);
#4
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 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
#5
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( ~, &, ^, )
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
#6
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?
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
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 ifstatement a while back.
@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 ifstatement a while back.
#8
Re: Increment a number without using any mathamatical Operator
Posted 15 August 2008  07:49 AM
That was an enlightening thread.
Page 1 of 1
