Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!

Addition using only bit manipulation

Aug
2,320
111
To keep my mind active,
here's my method of addition
using only bit manipulation.

Code:
@setlocal
@echo off
set num1=1989
set num2=35
do while %num2 ne 0
  set carry=%@eval[%num1 & %num2]
  set num1=%@eval[%num1 ^ %num2]
  set num2=%@eval[%carry << 1]
  echo carry: %carry
  echo  num1: %num1
  echo  num2: %num2
  pause
enddo
echo num1: %num1
endlocal

Output;
Code:
R:\>test.btm
carry: 1
 num1: 2022
 num2: 2
Press any key when ready...
carry: 2
 num1: 2020
 num2: 4
Press any key when ready...
carry: 4
 num1: 2016
 num2: 8
Press any key when ready...
carry: 0
 num1: 2024
 num2: 0
Press any key when ready...
num1: 2024

Joe
 
Last edited:
@Joe Caverly - if you like this kind of exercise and you like old HP calculators, try looking at the user forum at The Museum of HP Calculators (The Museum of HP Calculators). They post exercises like this in the forum there all the time!

If you don't have an HP calculator handy -- first, may I ask why not?! -- there are several software versions out there avalable for Android phones, iPhones, and PCs. "Simulators" for the pragmatists (individuals who wrote code from scratch, striving to get it to behave like the real thing), and "emulators" for the purists (software that emulates the HP hardware, and that runs the same BIOS that the actual hardware did. (How one gets the BIOS image and what one does with it is outside of my scope of discussion, and I am not a lawyer so I cannot offer advice on any legal details, boundaries, or ramifications.)


[ObligatoryDisclaimer: yes, I am an ubernerd and have a collection of about a dozen and a half of these machines: 11c, 15c, 16c, 32sII, 48gx, 50g, 48s, 41cx, et cetera. It was a safer and less expensive midlife crisis outlet than the red convertible.]
 
Hey @forbin,
Thanks for the link.

When I get the urge to do math "old school",
the TI emulators work great.

Just wish I could do the data entry with the keyboard,
instead of using the mouse.

Joe
 
Back
Top