Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help: bitwise opetator

Status
Not open for further replies.

TheEyez81

Technical User
Apr 11, 2007
7
0
0
US
I am trying to convert a simple segment of code from java to perl but the result is so different that I haven't been able to figure out what the cause is.

Java Code:
int aA[] = {49, 112, 251, 146, 1, 240};
char aB[] = {'N', 'o', 't', '4', 'M', 'e'};
for (int i=0; i<6; i++) {
System.out.println((char)aA^aB);
}
Java Result: 127,31,143,166,76,149

Perl Code:
@aA = ('49', '112', '251', '146', '1', '240');
@aB = ('N', 'o', 't', '4', 'M', 'e');
for ($i=0; $i<6; $i++) {
$result = $aA[$i] ^ $aB[$i];
print ord($result), "\n";
}
Perl Result: 122,94,70,5,124,87

 
You're applying ord() to the result when you don't need to.
I also changed the @aA array so that the elements are numeric rather than strings, and the @aB array so that it converts characters to their ascii code.

Code:
[blue]@aA[/blue] = [red]([/red][fuchsia]49[/fuchsia], [fuchsia]112[/fuchsia], [fuchsia]251[/fuchsia], [fuchsia]146[/fuchsia], [fuchsia]1[/fuchsia], [fuchsia]240[/fuchsia][red])[/red][red];[/red]
[blue]@aB[/blue] = [url=http://perldoc.perl.org/functions/map.html][black][b]map[/b][/black][/url] [red]{[/red][url=http://perldoc.perl.org/functions/ord.html][black][b]ord[/b][/black][/url][red]([/red][red])[/red][red]}[/red] [red]([/red][red]'[/red][purple]N[/purple][red]'[/red], [red]'[/red][purple]o[/purple][red]'[/red], [red]'[/red][purple]t[/purple][red]'[/red], [red]'[/red][purple]4[/purple][red]'[/red], [red]'[/red][purple]M[/purple][red]'[/red], [red]'[/red][purple]e[/purple][red]'[/red][red])[/red][red];[/red]

[olive][b]for[/b][/olive] [red]([/red][blue]$i[/blue]=[fuchsia]0[/fuchsia][red];[/red] [blue]$i[/blue]<[fuchsia]6[/fuchsia][red];[/red] [blue]$i[/blue]++[red])[/red] [red]{[/red]
  [blue]$result[/blue] = [blue]$aA[/blue][red][[/red][blue]$i[/blue][red]][/red] ^ [blue]$aB[/blue][red][[/red][blue]$i[/blue][red]][/red][red];[/red]
  [url=http://perldoc.perl.org/functions/print.html][black][b]print[/b][/black][/url] [blue]$result[/blue], [red]"[/red][purple], [/purple][red]"[/red][red];[/red]
[red]}[/red]
 
Couldn't help but to reduce it some more...
Code:
@aA = (49, 112, 251, 146, 1, 240);
@aB = map {ord()} ('N', 'o', 't', '4', 'M', 'e');
print join "\n", map {$aA[$_] ^ $aB[$_]} (0..5);
 
yea, in the JAVA code the numbers are getting converted to characters:

(char)aA

almost the same in perl:

Code:
@aA = (49, 112, 251, 146, 1, 240);
@aB = ('N', 'o', 't', '4', 'M', 'e');

for ($i=0; $i<6; $i++) {
  print $aA[$i] ^ ord($aB[$i]),"\n"
}

but the characters are converted to their ascii numeric equivalency then added together.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Thanks for the help.. so quick too.
convert @aA to chr, should be the same correct?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top