cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
%3CLINGO-SUB%20id%3D%22lingo-sub-559280%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3EHow%20do%20I%20perform%20bitwise%20xor%20operations%20between%20columns%20with%2044-bit%20binary%20data%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-559280%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3EI%20want%20to%20perform%20bitwise%20xor%20operations%20between%20two%20columns%20and%20output%20the%20result%20in%20the%203rd%20column.%20Please%20note%20that%20the%20values%20are%2044%20bits%20long%20in%20both%20columns.%3CBR%20%2F%3E%3CBR%20%2F%3EPlease%20assume%20as%20an%20example%20the%20values%20are%201s%20on%20one%20column%20and%200s%20on%20the%20other%2C%2044%20bits%20long.%20Can%20you%20explain%20how%20I%20can%20achieve%20this%20with%20a%20formula%20or%20a%20jsl%20script%3F%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-559280%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3E%3CLINGO-LABEL%3EAutomation%20and%20Scripting%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-559288%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3ERe%3A%20How%20do%20I%20perform%20bitwise%20xor%20operations%20between%20columns%20with%2044-bit%20binary%20data%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-559288%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3E%3CP%3EExtending%20Jim's%20approach%20of%20using%20add%20for%20XOR%2C%20and%20using%20some%20matrix%20functions%20to%20remove%20the%20loops...and%20an%20ASCII%20table.%3C%2FP%3E%0A%3CP%3EThese%20are%20untested%2C%20make%20sure%20they%20do%20what%20you%20expect.%3C%2FP%3E%0A%3CPRE%3E%3CCODE%20class%3D%22%20language-jsl%22%3Echar_xor%20%3D%20Function(%20%7Ba%2C%20b%7D%2C%20%7Baa%2C%20bb%7D%2C%20%0A%20%20%20%20aa%20%3D%20Blob%20To%20Matrix(%20Char%20To%20Blob(%20a%20)%2C%20%22int%22%2C%201%2C%20%22little%22%20)%3B%20%2F%2F%20string%20to%20matrix...%0A%20%20%20%20bb%20%3D%20Blob%20To%20Matrix(%20Char%20To%20Blob(%20b%20)%2C%20%22int%22%2C%201%2C%20%22little%22%20)%3B%20%2F%2F%20%220%22%20-%26gt%3B%2048%2C%20%221%22%20-%26gt%3B%2049%0A%20%20%20%20cc%20%3D%20Mod(%20aa%20%2B%20bb%2C%202%20)%3B%20%2F%2F%20XOR%2C%2048%2B48-%26gt%3B0%2C%2048%2B49-%26gt%3B1%2C%2049%2B49-%26gt%3B0%0A%20%20%20%20Blob%20To%20Char(%20Matrix%20To%20Blob(%20cc%20%2B%2048%2C%20%22int%22%2C%201%2C%20%22little%22%20)%20)%3B%20%2F%2F%200-%26gt%3B48%2C%201-%26gt%3B49%0A)%3B%0Achar_or%20%3D%20Function(%20%7Ba%2C%20b%7D%2C%20%7Baa%2C%20bb%7D%2C%20%0A%20%20%20%20aa%20%3D%20Blob%20To%20Matrix(%20Char%20To%20Blob(%20a%20)%2C%20%22int%22%2C%201%2C%20%22little%22%20)%3B%20%2F%2F%20string%20to%20matrix...%0A%20%20%20%20bb%20%3D%20Blob%20To%20Matrix(%20Char%20To%20Blob(%20b%20)%2C%20%22int%22%2C%201%2C%20%22little%22%20)%3B%20%2F%2F%20%220%22%20-%26gt%3B%2048%2C%20%221%22%20-%26gt%3B%2049%0A%20%20%20%20cc%20%3D%20aa%20%2B%20bb%20!%3D%2096%3B%20%2F%2F%20OR%2C%2096%20is%20%220%22%2B%220%22%20%3D%3D%2048%2B48%0A%20%20%20%20Blob%20To%20Char(%20Matrix%20To%20Blob(%20cc%20%2B%2048%2C%20%22int%22%2C%201%2C%20%22little%22%20)%20)%3B%20%2F%2F%200-%26gt%3B48%2C%201-%26gt%3B49%0A)%3B%0Achar_and%20%3D%20Function(%20%7Ba%2C%20b%7D%2C%20%7Baa%2C%20bb%7D%2C%20%0A%20%20%20%20aa%20%3D%20Blob%20To%20Matrix(%20Char%20To%20Blob(%20a%20)%2C%20%22int%22%2C%201%2C%20%22little%22%20)%3B%20%2F%2F%20string%20to%20matrix...%0A%20%20%20%20bb%20%3D%20Blob%20To%20Matrix(%20Char%20To%20Blob(%20b%20)%2C%20%22int%22%2C%201%2C%20%22little%22%20)%3B%20%2F%2F%20%220%22%20-%26gt%3B%2048%2C%20%221%22%20-%26gt%3B%2049%0A%20%20%20%20cc%20%3D%20Mod(%20aa%2C%202%20)%20%3CLI-EMOJI%20id%3D%22lia_face-blowing-a-kiss%22%20title%3D%22%3Acara_que_env%C3%ADa_un_beso%3A%22%3E%3C%2FLI-EMOJI%3E%20Mod(%20bb%2C%202%20)%3B%20%2F%2F%20AND%2C%20%3CLI-EMOJI%20id%3D%22lia_face-blowing-a-kiss%22%20title%3D%22%3Acara_que_env%C3%ADa_un_beso%3A%22%3E%3C%2FLI-EMOJI%3E%20is%20element-wise.%20multiply%200*0%2C%201*0%2C%200*1%2C%201*1%0A%20%20%20%20Blob%20To%20Char(%20Matrix%20To%20Blob(%20cc%20%2B%2048%2C%20%22int%22%2C%201%2C%20%22little%22%20)%20)%3B%20%2F%2F%200-%26gt%3B48%2C%201-%26gt%3B49%0A)%3B%0Achar_not%20%3D%20Function(%20%7Ba%7D%2C%0A%20%20%20%20Substitute(%20a%2C%20%220%22%2C%20%22*%22%2C%20%221%22%2C%20%220%22%2C%20%22*%22%2C%20%221%22%20)%3B%20%2F%2F%20NOT%0A)%3B%0A%0A%2F%2F%20tests%0A%0AShow(%20char_xor(%20%220011%22%2C%20%220101%22%20)%20%3D%3D%20%220110%22%2C%0A%20%20%20%20%20%20char_or(%20%220011%22%2C%20%220101%22%20)%20%3D%3D%20%220111%22%2C%0A%20%20%20%20%20%20char_and(%20%220011%22%2C%20%220101%22%20)%20%3D%3D%20%220001%22%2C%0A%20%20%20%20%20%20char_not(%20%2201%22%20)%20%3D%3D%20%2210%22%20)%3B%0A%0Ap%20%3D%20%2200110000011111%22%3B%0Aq%20%3D%20%2201010101011100%22%3B%0A%0AShow(%20p%2C%20q%2C%20char_not(%20char_and(%20p%2C%20q%20)%20)%20%3D%3D%20char_or(%20char_not(%20p%20)%2C%20char_not(%20q%20)%20)%20)%3B%20%2F%2F%20De%20Morgan's%20Theorem%0A%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3EYou%20might%20want%20error%20checks%20for%20bad%20characters%20like%20%22010130101%22.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EWow.%20Substitute%20did%20not%20work%20the%20way%20I%20thought%20it%20would.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-559282%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3ERe%3A%20How%20do%20I%20perform%20bitwise%20xor%20operations%20between%20columns%20with%2044-bit%20binary%20data%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-559282%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3E%3CP%3EHere%20is%20one%20way%20to%20handle%20the%20problem%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22txnelson_0-1666406596889.png%22%20style%3D%22width%3A%20714px%3B%22%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%22%20image-alt%3D%22txnelson_0-1666406596889.png%22%20style%3D%22width%3A%20714px%3B%22%3E%3Cspan%20class%3D%22lia-inline-image-display-wrapper%22%20image-alt%3D%22txnelson_0-1666406596889.png%22%20style%3D%22width%3A%20714px%3B%22%3E%3Cimg%20src%3D%22https%3A%2F%2Fcommunity.jmp.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F46503i1CB3D552679031CC%2Fimage-dimensions%2F714x250%3Fv%3Dv2%22%20width%3D%22714%22%20height%3D%22250%22%20role%3D%22button%22%20title%3D%22txnelson_0-1666406596889.png%22%20alt%3D%22txnelson_0-1666406596889.png%22%20%2F%3E%3C%2Fspan%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CPRE%3E%3CCODE%20class%3D%22%20language-jsl%22%3EFor(%20i%20%3D%201%2C%20i%20%26lt%3B%3D%201%2C%20i%20%3D%20i%20%2B%2015%2C%0A%20loops%20%3D%20Ceiling(%20Length(%20%3AA%20)%20%2F%2015%20)%3B%0A%20val%20%3D%20%22%22%3B%0A%20For(%20i%20%3D%201%2C%20i%20%26lt%3B%3D%20Length(%20%3AA%20)%2C%20i%20%3D%20i%20%2B%2015%2C%0A%20%20Asub%20%3D%20Substr(%20%3AA%2C%20i%2C%2015%20)%3B%0A%20%20absLength%20%3D%20Length(%20Asub%20)%3B%0A%20%20zeros%20%3D%20Repeat(%20%220%22%2C%20absLength%20)%3B%0A%20%20Bsub%20%3D%20Substr(%20%3AB%2C%20i%2C%2015%20)%3B%0A%20%20Csub%20%3D%20Sum(%20Num(%20Asub%20)%2C%20Num(%20Bsub%20)%20)%3B%0A%20%20Csub%20%3D%20Format(%20Csub%2C%20%22Fixed%20Dec%22%2C%2015%2C%200%20)%3B%0A%20%20If(%20Length(%20Csub%20)%20!%3D%20absLength%2C%0A%20%20%20Csub%20%3D%20Substr(%20zeros%2C%201%2C%20absLength%20-%20Length(%20Csub%20)%20)%20%7C%7C%20Csub%0A%20%20)%3B%0A%20%20val%20%3D%20val%20%7C%7C%20Csub%3B%0A%20)%3B%0A%20val%20%3D%20Substitute(%20val%2C%20%222%22%2C%20%220%22%20)%3B%0A)%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E
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