Bits, Bytes and Binaries
Posted: Fri Aug 07, 2009 2:26 pm
by Jens Richnow >> Thu, 22 Jun 2000 0:08:44 GMT
Hi,
for some encoding/decoding routines I need to do lots of bitwise AND operations of two Integers. Since JADE does not have a Byte primitive type and the Binary gives a hexadecimal representation in 4 byte blocks, I ventured to write some methods to achieve this bitwise AND operation. This, however, required four method calls (actually three without the initiating method) and some string manipulations. Therefore, the encoding/decoding of large strings requires some time in JADE. I attach the methods I just below and wonder whether somebody has a better idea with handling bytes and bits in JADE and do operations with them.
Cheers
Jens
The first method sits on the Binary primitive type and the other three can be tested using JadeScript by running the ::bits() method.
Binary::asByte():String updating; //========================================================================== // Purpose: Converts a byte to its binary representation, e.g., 0101010. // Return: Binary representation as String type.
// Created: 21 June 2000 by Jens //========================================================================== vars count :Integer;
byte :Integer;
bits :String;begin
byte := self[1:1].Integer;
foreach count in 7 to 0 step -1 do
if (2 ^ count) <= byte then
bits := bits & "1";
byte := byte - (2 ^ count);
else
bits := bits & "0";
endif;
endforeach;
return bits;
end;
bits() updating; //========================================================================== // Purpose: Converts two Integers in the byte representation and
intiates a
// bitwise comparison.
// Created: 25 May 2000 by Jens. //========================================================================== vars bin1, bin2 :Binary;
int1, int2 :Integer;
str1, str2, str3 :String;begin
read int1;
read int2;
bin1 := int1.Binary;
bin2 := int2.Binary;
str1 := bin1.asByte();
str2 := bin2.asByte();
write str1 & Tab & "= " & bitToInteger(str1).String;
write str2 & Tab & "= " & bitToInteger(str2).String & CrLf;
str3 := bitwiseAND(bin1.asByte, bin2.asByte);
write str3 & Tab & "= " & bitToInteger(str3).String;
end;
bitwiseAND(byte1, byte2:String):String updating; //========================================================================== // Purpose: Bitwise AND comparison of two byte.
// Return: Result.
// Created: 21 June 2000 by Jens //========================================================================== vars bit :Integer;
andBit :String;begin
foreach bit in 1 to 8 do
if byte1[bit:1] = byte2[bit:1] then
if byte1[bit:1] = "1" or byte2[bit:1] = "1" then
andBit := andBit & "1";
else
andBit := andBit & "0";
endif;
else
andBit := andBit & "0";
endif;
endforeach;
return andBit;
end;
byteToInteger(byte:String):Integer updating; //========================================================================== // Purpose: Converts a byte representation to an integer.
// Created: 21 June 2000 by Jens //========================================================================== vars count :Integer;
result :Integer;begin
result := 0;
// Left to right.
foreach count in 1 to 8 do
if count = 1 and byte[count:1] = "1" then
result := result + 128;
elseif count = 2 and byte[count:1] = "1" then
result := result + 64;
elseif count = 3 and byte[count:1] = "1" then
result := result + 32;
elseif count = 4 and byte[count:1] = "1" then
result := result + 16;
elseif count = 5 and byte[count:1] = "1" then
result := result + 8;
elseif count = 6 and byte[count:1] = "1" then
result := result + 4;
elseif count = 7 and byte[count:1] = "1" then
result := result + 2;
elseif count = 8 and byte[count:1] = "1" then
result := result + 1;
endif;
endforeach;
return result;
end;
Hi,
for some encoding/decoding routines I need to do lots of bitwise AND operations of two Integers. Since JADE does not have a Byte primitive type and the Binary gives a hexadecimal representation in 4 byte blocks, I ventured to write some methods to achieve this bitwise AND operation. This, however, required four method calls (actually three without the initiating method) and some string manipulations. Therefore, the encoding/decoding of large strings requires some time in JADE. I attach the methods I just below and wonder whether somebody has a better idea with handling bytes and bits in JADE and do operations with them.
Cheers
Jens
The first method sits on the Binary primitive type and the other three can be tested using JadeScript by running the ::bits() method.
Binary::asByte():String updating; //========================================================================== // Purpose: Converts a byte to its binary representation, e.g., 0101010. // Return: Binary representation as String type.
// Created: 21 June 2000 by Jens //========================================================================== vars count :Integer;
byte :Integer;
bits :String;begin
byte := self[1:1].Integer;
foreach count in 7 to 0 step -1 do
if (2 ^ count) <= byte then
bits := bits & "1";
byte := byte - (2 ^ count);
else
bits := bits & "0";
endif;
endforeach;
return bits;
end;
bits() updating; //========================================================================== // Purpose: Converts two Integers in the byte representation and
intiates a
// bitwise comparison.
// Created: 25 May 2000 by Jens. //========================================================================== vars bin1, bin2 :Binary;
int1, int2 :Integer;
str1, str2, str3 :String;begin
read int1;
read int2;
bin1 := int1.Binary;
bin2 := int2.Binary;
str1 := bin1.asByte();
str2 := bin2.asByte();
write str1 & Tab & "= " & bitToInteger(str1).String;
write str2 & Tab & "= " & bitToInteger(str2).String & CrLf;
str3 := bitwiseAND(bin1.asByte, bin2.asByte);
write str3 & Tab & "= " & bitToInteger(str3).String;
end;
bitwiseAND(byte1, byte2:String):String updating; //========================================================================== // Purpose: Bitwise AND comparison of two byte.
// Return: Result.
// Created: 21 June 2000 by Jens //========================================================================== vars bit :Integer;
andBit :String;begin
foreach bit in 1 to 8 do
if byte1[bit:1] = byte2[bit:1] then
if byte1[bit:1] = "1" or byte2[bit:1] = "1" then
andBit := andBit & "1";
else
andBit := andBit & "0";
endif;
else
andBit := andBit & "0";
endif;
endforeach;
return andBit;
end;
byteToInteger(byte:String):Integer updating; //========================================================================== // Purpose: Converts a byte representation to an integer.
// Created: 21 June 2000 by Jens //========================================================================== vars count :Integer;
result :Integer;begin
result := 0;
// Left to right.
foreach count in 1 to 8 do
if count = 1 and byte[count:1] = "1" then
result := result + 128;
elseif count = 2 and byte[count:1] = "1" then
result := result + 64;
elseif count = 3 and byte[count:1] = "1" then
result := result + 32;
elseif count = 4 and byte[count:1] = "1" then
result := result + 16;
elseif count = 5 and byte[count:1] = "1" then
result := result + 8;
elseif count = 6 and byte[count:1] = "1" then
result := result + 4;
elseif count = 7 and byte[count:1] = "1" then
result := result + 2;
elseif count = 8 and byte[count:1] = "1" then
result := result + 1;
endif;
endforeach;
return result;
end;