Für einen Perl-Checker: Variable bitweise verknüpfen
Micha
- perl
0 Cheatah0 Micha0 Michael Schröpl0 timothy0 Micha
Hi!
Ich habe folgendes Problem: Ich will eine binäre Zahl mit einer anderen Zahl bitweise ODER- verknüpfen , weiß aber leider nicht so ganz wie ich das machen muß.
#!/usr/bin/perl
$a=11010;
$a=$a 00100;
print "$a \n";
Bei Programmausführung wird 11074 ausgegeben. Wie kommt es zu diesem Wert?
Ich möchte, daß die Variable $a in oberem Beispiel am Schluß den Wert 11110 enthält. Wie muß ich das anstellen?
Ciao
Micha
Hi,
Ich habe folgendes Problem: Ich will eine binäre Zahl mit einer anderen Zahl bitweise ODER- verknüpfen , weiß aber leider nicht so ganz wie ich das machen muß.
Dein Problem ist das Wort vor "Zahl".
#!/usr/bin/perl
$a=11010;
$a ist jetzt also elf tausend und zehn.
$a=$a 00100;
Es wird oder-verknüpft mit einer Zahl, die mit der Ziffer "0" beginnt, also mit einer oktalen Zahl; in diesem Fall mit der 64.
print "$a \n";
Das Ergebnis wird korrekt ausgegeben.
Ich möchte, daß die Variable $a in oberem Beispiel am Schluß den Wert 11110 enthält. Wie muß ich das anstellen?
#!/usr/bin/perl -w
use strict;
my $a = 26;
$a = 4;
print $a, "\n";
Wie Du aus einer dezimalen eine binäre Zahl machst (und umgekehrt), überlasse ich Deiner Kreativität ;-) Aber jetzt weißt Du erst mal, woran es liegt. Hoffe ich :-)
Cheatah
Hi!
Cheatah , Vielen Dank für Deine schnelle Hilfe!
Sowas wie bei Hexzahlen mit "0x" davor oder "0" bei Oktalzahlen - gibt's das auch für binär?
Wenn "Nein" muß ich die Umrechnung von dezimalen in eine binäre Zahl (und umgekehrt) selbst schreiben oder gibt's einen Befehl?
Ciao
Micha
Sowas wie bei Hexzahlen mit "0x" davor oder "0" bei Oktalzahlen - gibt's das auch für binär?
Wenn "Nein" muß ich die Umrechnung von dezimalen in eine binäre Zahl (und umgekehrt) selbst schreiben oder gibt's einen Befehl?
Falls Du bei den Datentyp-Kürzeln von "sprintf" nichts Passendes findest (sollte es eigentlich geben - habe gerade kein Perl zur Hand), dann wäre eine Funktion, welche durch (Schleife) fortgesetztes MODULO 2 und Halbieren des ganzzahligen Anteils die Binärzahl von rechts nach links aufbaut, ungefähr ein Dreizeiler.
Wenn "Nein" muß ich die Umrechnung von dezimalen in eine binäre Zahl (und umgekehrt) selbst schreiben oder gibt's einen Befehl?
Hi Micha,
ich habe dir mal kurz ein "Rezept" aus dem "Perl - Cookbook" geschnippt. Vielleicht hilft dir dies ja weiter.
Bye
Timothy
2.4. Converting Between Binary and Decimal
Problem
You have an integer whose binary representation you'd like to print out, or a binary representation that you'd like to convert into an integer. You might want to do this if you were displaying non-textual data, such as what you get from interacting with certain system programs and functions.
Solution
To convert a Perl integer to a text string of ones and zeros, first pack the integer into a number in network byte order[1] (the "N" format), then unpack it again bit by bit (the "B32" format).
[1] Also known as big-endian, or MSB (Most-Significant Bit first) order.
sub dec2bin {
my $str = unpack("B32", pack("N", shift));
$str =~ s/^0+(?=\d)//; # otherwise you'll get leading zeros
return $str;
}
To convert a text string of ones and zeros to a Perl integer, first massage the string by padding it with the right number of zeros, then just reverse the previous procedure.
sub bin2dec {
return unpack("N", pack("B32", substr("0" x 32 . shift, -32)));
}
Discussion
We're talking about converting between strings like "00100011" and numbers like 35. The string is the binary representation of the number. We can't solve either problem with sprintf (which doesn't have a "print this in binary" format), so we have to resort to Perl's pack and unpack functions for manipulating strings of data.
The pack and unpack functions act on strings. You can treat the string as a series of bits, bytes, integers, long integers, floating-point numbers in IEEE representation, checksums - among other strange things. The pack and unpack functions both take formats, like sprintf, specifying what they should do with their arguments.
We use pack and unpack in two ways: "treat this string as a series of bits" and "treat this string as containing a binary representation of an integer." When we treat the string as a series of bits, we have to understand how pack will behave. Such a string is treated as a series of bytes, a byte being eight bits. The bytes are always counted from left to right (the first eight bits are the first byte, the next eight bits are the second, and so on), but the bits within each byte can be counted left-to-right as well as right-to-left.
We use pack with a template of "B" to work with bits within each byte from left to right. This is the order that the "N" format expects them in, which we use to treat the series of bits as representing a 32-bit integer.
$num = bin2dec('0110110'); # $num is 54
$binstr = dec2bin(54); # $binstr is 110110
Hi Timothy!
Thanx für Dein "Rezept" aus dem "Perl - Cookbook"!
Hat mir auf jeden Fall sehr geholfen.
Ciao
Micha