|
Turbomudder Beginner
Joined: 25 Jan 2003 Posts: 19
|
Posted: Fri Jan 31, 2003 6:21 am
%bitor, binary addition |
I need to add some 20 digit binary numbers... %bitor would be perfect, but if I use a number more than 12 digits long, it gives an error message that says "this is not a valid integer"
also, I am trying to get the result returned in binary form, and very strangely it seems that if i add numbers 4 or more digits long, I get answers in decimal form. It all seems quite an overcomplicated situation when all I really want to do is add some 20 digit binary numbers.
like :
11111000001111100000 +
00011111000001111100 =
11111111001111111100
any suggestions?
thanks,
turbomudder. |
|
|
|
Charbal GURU
Joined: 15 Jun 2001 Posts: 654 Location: USA
|
Posted: Fri Jan 31, 2003 9:52 am |
The %bit* functions can't be used directly here since they accept decimal numbers as their inputs (even though they perform operations on the binary bits of these number).
There are two basic ways to go about this one. You can convert your numbers to decimal and then add them and convert them back or you could implement functions to add the bits in binary strings. The first method is likely better in this case.
#CLASS BinaryAdd
#VAR Output %null
#VAR Output2 %null
#VAR Output3 %null
#ALIAS binadd {bintodec %1;#VAR Output2 @Output;bintodec %2;#VAR Output3 %eval(@Output+@Output2);dectobin @Output3;#SAY The result is @Output}
#ALIAS bintodec {#VAR Output 0;#LOOP 1,%len("%-1") {#MATH Output @Output*2;#ADD Output %copy("%-1", %i, 1)}}
#ALIAS dectobin {#VAR Output "";#VAR Output2 {%-1};#WHILE (%len(@Output2) <> 0 and @Output2 <> 0) {#VAR Output %concat(%bitand(@Output2, 1), @Output);#ADD Output2 -%bitand(@Output2, 1);#VAR Output2 %eval(@Output2/2)}}
#CLASS 0
That will perform addition on your numbers. If we are talking about addition, your example is incorrect...
11111000001111100000 +
00011111000001111100 =
100010111010001011100
So your example is actually showing a bitwise or. My aliases are such that it's easy to modify them for this. Just add:
#ALIAS binor {bintodec %1;#VAR Output2 @Output;bintodec %2;#VAR Output3 %eval(%bitor(@Output, @Output2));dectobin @Output3;#SAY The result is @Output}
- Charbal |
|
|
|
Charbal GURU
Joined: 15 Jun 2001 Posts: 654 Location: USA
|
Posted: Fri Jan 31, 2003 10:05 am |
As an aside, %bitor seems to be bugged in the current beta (6.53) so if you are using that, it may not work correctly.
- Charbal |
|
|
|
Turbomudder Beginner
Joined: 25 Jan 2003 Posts: 19
|
Posted: Fri Jan 31, 2003 10:36 am |
*nod* I didnt mean addition technically, bitwise or is what i meant... the thing is, i actually already made functions to do bitwise or on the numbers using a loop, but i need to do this a couple of hundred times, and the loop takes rather a long time, so i was really hoping to use a predefined function instead that would be much simpler and so faster. I guess ill have to find a way to use the decimal representations instead...
Turbomudder |
|
|
|
|
|
|
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
|
|