Login [Register]
Don't have an account? Register now to chat, post, use our tools, and much more.
Does anyone have some code that performs a bitwise XOR operation on two numbers?

Thanks in advance Smile
If you were able to put the bin in a string with DCS7 commands, you could do something like:


Code:
:" "→str3
:For(A,1,length(str1
:expr(sub(str1,A,1→B
:expr(sub(str2,A,1→C
:If B xor C
:Then
:str3+"1"→str3
:Else
:str3+"0"→str3
:End:End
:Sub(str3,2,length(str3)-1→str3

I fixed it! It furs out I was using the wrong variable in the code that called this program, here's the source in case anyone would like to see it:


Code:
8->F
While F<S or F<E
F*2->F
End
0->R
For(A,1,F)
int(2fPart(.5N/2^(A-1->D
int(2fPart(.5Z/2^(A-1->L
If A<=E and A>=S and (D xor L):R+(2^(A-1))->R
End


Variables:
N = 1st number
Z = 2nd number
R = Output
S = Starting bit to calculate from
E = Ending bit to calculate to
I came up with the following technique, but I know Weregoose is about to ninja this post with the code he posted on IRC after you logged off last night. My version performs A xor B -> C. Shameless fact: I ripped off the complex number concept from Weregoose.


Code:
sum(seq(.5(2^X)(1=abs(int(2fPart((A+Bi)/(2^X))))),X,1,~min(int(~log(A)/log(2)),int(~log(B)/log(2


I am sure I can shrink the code that figures out how many bits to compute. Anyway, here's how that works:

1) Computes the number of binary digits that will need to be generated: ~min(int(~log(A)/log(2)),int(~log(B)/log(2. This uses a pair of change of base operations plus a quirk of the int() command to get the ceiling of the two numbers of digits, and effectively gets the max of the two.
2) Computes a complex number for each bit indicating the value of the bit in A and B: fPart((A+Bi)/(2^X)).
3) Make sure that it's either 1+0i or 0+1i: 1=abs(int(2...))
4) Give each resulting bit its proper place value: .5(2^X)
5) Generate a list of place-valued bits and sum them: sum(seq(
Wow, thanks! I'll post my other bitwise operation code (AND, Shift, OR), I'm sure you'll come up with a one liner for those Very Happy
Let's see how similar these end up being...

Code:
sum(seq(2^Xabs(1=abs(int(2fPart(.5/2^X(A+Bi))))),X,0,log(2)⁻¹log(max({A,B}


If you want to retool this into another bitwise operation, go to the "=" and substitute it with "<" for AND, or "≤" for OR.

[EDIT]

Three fewer bytes and noticeably faster:

Code:
:int(log(2)⁻¹log(max({A,B
:2^cumSum(binomcdf(Ans,0
:sum(Ans.5(1=abs(int(2fPart(Ans⁻¹(A+Bi
Wow, that is fantastic!
I have one question though, what is the ⁻ symbol?
the emulator won't accept it.

EDIT: Found it on catalog
SamTebbs33 wrote:
Wow, that is fantastic!
I have one question though, what is the ⁻ symbol?
the emulator won't accept it.

EDIT: Found it on catalog

It comes from the x⁻ key, right below MATH and above the x-squared key.
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 1
» All times are GMT - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement