Recommended Posts

I've been trying to brush up on my bit math skills and I ran into an interesting practice excercise. Would anyone know how to add 1 to a number without using the + or - operators and using only bitwise operations? I ended up writing this multiple-line/slow/intuitive algorithm but is there like a some fancy/fast/unintuitive trick to doing it in one line.
Thanks,

0

Share this post

Link to post

Share on other sites

Binary digit addition (as any other addition, too) could produce an overflow that has to be propagated to the next higher digit. So a digit shows an inherent dependency on all lower digits. That can be solved either by an iterative process or else a growing look-ahead functionality (or else a combination of those).

Since you're adding 1 only, the propagation is to be done as long as the current digit of the number is 1. E.g.:

Share this post

Link to post

Share on other sites

Any source on beginer digital logic should have a section on this. Its like day three. Your basic logic operations, simplifying logic operations, implementing addition using logic operations. So I'd start there.