cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Choose Language Hide Translation Bar
psundar6
Level III

How do I perform bitwise xor operations between columns with 44-bit binary data?

I want to perform bitwise xor operations between two columns and output the result in the 3rd column. Please note that the values are 44 bits long in both columns.

Please assume as an example the values are 1s on one column and 0s on the other, 44 bits long. Can you explain how I can achieve this with a formula or a jsl script?
1 ACCEPTED SOLUTION

Accepted Solutions
Craige_Hales
Super User

Re: How do I perform bitwise xor operations between columns with 44-bit binary data?

Extending Jim's approach of using add for XOR, and using some matrix functions to remove the loops...and an ASCII table.

These are untested, make sure they do what you expect.

char_xor = Function( {a, b}, {aa, bb}, 
    aa = Blob To Matrix( Char To Blob( a ), "int", 1, "little" ); // string to matrix...
    bb = Blob To Matrix( Char To Blob( b ), "int", 1, "little" ); // "0" -> 48, "1" -> 49
    cc = Mod( aa + bb, 2 ); // XOR, 48+48->0, 48+49->1, 49+49->0
    Blob To Char( Matrix To Blob( cc + 48, "int", 1, "little" ) ); // 0->48, 1->49
);
char_or = Function( {a, b}, {aa, bb}, 
    aa = Blob To Matrix( Char To Blob( a ), "int", 1, "little" ); // string to matrix...
    bb = Blob To Matrix( Char To Blob( b ), "int", 1, "little" ); // "0" -> 48, "1" -> 49
    cc = aa + bb != 96; // OR, 96 is "0"+"0" == 48+48
    Blob To Char( Matrix To Blob( cc + 48, "int", 1, "little" ) ); // 0->48, 1->49
);
char_and = Function( {a, b}, {aa, bb}, 
    aa = Blob To Matrix( Char To Blob( a ), "int", 1, "little" ); // string to matrix...
    bb = Blob To Matrix( Char To Blob( b ), "int", 1, "little" ); // "0" -> 48, "1" -> 49
    cc = Mod( aa, 2 )  Mod( bb, 2 ); // AND,  is element-wise. multiply 0*0, 1*0, 0*1, 1*1
    Blob To Char( Matrix To Blob( cc + 48, "int", 1, "little" ) ); // 0->48, 1->49
);
char_not = Function( {a},
    Substitute( a, "0", "*", "1", "0", "*", "1" ); // NOT
);

// tests

Show( char_xor( "0011", "0101" ) == "0110",
      char_or( "0011", "0101" ) == "0111",
      char_and( "0011", "0101" ) == "0001",
      char_not( "01" ) == "10" );

p = "00110000011111";
q = "01010101011100";

Show( p, q, char_not( char_and( p, q ) ) == char_or( char_not( p ), char_not( q ) ) ); // De Morgan's Theorem

You might want error checks for bad characters like "010130101".

 

Wow. Substitute did not work the way I thought it would.

 

Craige

View solution in original post

2 REPLIES 2
txnelson
Super User

Re: How do I perform bitwise xor operations between columns with 44-bit binary data?

Here is one way to handle the problem

txnelson_0-1666406596889.png

For( i = 1, i <= 1, i = i + 15,
	loops = Ceiling( Length( :A ) / 15 );
	val = "";
	For( i = 1, i <= Length( :A ), i = i + 15,
		Asub = Substr( :A, i, 15 );
		absLength = Length( Asub );
		zeros = Repeat( "0", absLength );
		Bsub = Substr( :B, i, 15 );
		Csub = Sum( Num( Asub ), Num( Bsub ) );
		Csub = Format( Csub, "Fixed Dec", 15, 0 );
		If( Length( Csub ) != absLength,
			Csub = Substr( zeros, 1, absLength - Length( Csub ) ) || Csub
		);
		val = val || Csub;
	);
	val = Substitute( val, "2", "0" );
)

 

 

Jim
Craige_Hales
Super User

Re: How do I perform bitwise xor operations between columns with 44-bit binary data?

Extending Jim's approach of using add for XOR, and using some matrix functions to remove the loops...and an ASCII table.

These are untested, make sure they do what you expect.

char_xor = Function( {a, b}, {aa, bb}, 
    aa = Blob To Matrix( Char To Blob( a ), "int", 1, "little" ); // string to matrix...
    bb = Blob To Matrix( Char To Blob( b ), "int", 1, "little" ); // "0" -> 48, "1" -> 49
    cc = Mod( aa + bb, 2 ); // XOR, 48+48->0, 48+49->1, 49+49->0
    Blob To Char( Matrix To Blob( cc + 48, "int", 1, "little" ) ); // 0->48, 1->49
);
char_or = Function( {a, b}, {aa, bb}, 
    aa = Blob To Matrix( Char To Blob( a ), "int", 1, "little" ); // string to matrix...
    bb = Blob To Matrix( Char To Blob( b ), "int", 1, "little" ); // "0" -> 48, "1" -> 49
    cc = aa + bb != 96; // OR, 96 is "0"+"0" == 48+48
    Blob To Char( Matrix To Blob( cc + 48, "int", 1, "little" ) ); // 0->48, 1->49
);
char_and = Function( {a, b}, {aa, bb}, 
    aa = Blob To Matrix( Char To Blob( a ), "int", 1, "little" ); // string to matrix...
    bb = Blob To Matrix( Char To Blob( b ), "int", 1, "little" ); // "0" -> 48, "1" -> 49
    cc = Mod( aa, 2 )  Mod( bb, 2 ); // AND,  is element-wise. multiply 0*0, 1*0, 0*1, 1*1
    Blob To Char( Matrix To Blob( cc + 48, "int", 1, "little" ) ); // 0->48, 1->49
);
char_not = Function( {a},
    Substitute( a, "0", "*", "1", "0", "*", "1" ); // NOT
);

// tests

Show( char_xor( "0011", "0101" ) == "0110",
      char_or( "0011", "0101" ) == "0111",
      char_and( "0011", "0101" ) == "0001",
      char_not( "01" ) == "10" );

p = "00110000011111";
q = "01010101011100";

Show( p, q, char_not( char_and( p, q ) ) == char_or( char_not( p ), char_not( q ) ) ); // De Morgan's Theorem

You might want error checks for bad characters like "010130101".

 

Wow. Substitute did not work the way I thought it would.

 

Craige