how to do bit complement in perl
how to do bit complement in perl
am 09.01.2008 20:20:06 von IJALAB
hi all,
i have a file in which there are 8 byte hex values one followed by
another. For example:
2A414364
00001DA9
01A3F9DD
3FFFF661
00EE6670
000011CF
2BC43FD0
3FEB0003
3F7FFF40
I need to check for the last bit in every data and if it being set,
then the next data's 24 bits have to be inverted(assuming last bit is
d31, starting from d26 to d7 needs to be inverted). For example:
1st data 0x2A414364 has last bit as '0' so the next word can be
retained as same.But, 00001DA9(2nd data), the last bit is '1' so the
next data 01A3F9DD will have to be printed as 3e5c061d. so, for the
upper set of data, the below data needs to be printed. how do i do bit
complement in hex using perl.
2a414364
00001da9
3e5c061d
000009a1
3f1199b0
000011cf
143bc010
3feb0003
00800080
regards,
bala
Re: how to do bit complement in perl
am 09.01.2008 20:43:33 von smallpond
On Jan 9, 2:20 pm, IJALAB wrote:
> hi all,
>
> i have a file in which there are 8 byte hex values one followed by
> another. For example:
> 2A414364
> 00001DA9
> 01A3F9DD
> 3FFFF661
> 00EE6670
> 000011CF
> 2BC43FD0
> 3FEB0003
> 3F7FFF40
>
> I need to check for the last bit in every data and if it being set,
> then the next data's 24 bits have to be inverted(assuming last bit is
> d31, starting from d26 to d7 needs to be inverted). For example:
> 1st data 0x2A414364 has last bit as '0' so the next word can be
> retained as same.But, 00001DA9(2nd data), the last bit is '1' so the
> next data 01A3F9DD will have to be printed as 3e5c061d. so, for the
> upper set of data, the below data needs to be printed. how do i do bit
> complement in hex using perl.
>
> 2a414364
> 00001da9
> 3e5c061d
> 000009a1
> 3f1199b0
> 000011cf
> 143bc010
> 3feb0003
> 00800080
>
> regards,
> bala
Test low bit
perl -e 'print hex( "2A414364") & 1 ? "odd" : "even";'
even
perl -e 'print hex( "00001DA9") & 1 ? "odd" : "even";'
odd
Complement whole word
perl -e 'printf "%08x",~hex( "00001DA9");'
ffffe256
XOR with mask
perl -e 'printf "%08x", 0x00ffff00 ^ hex( "01A3F9DD");'
015c06dd
Re: how to do bit complement in perl
am 09.01.2008 22:12:08 von Jim Gibson
In article
<8b9c4337-421b-4da3-919d-a05e78702786@f47g2000hsd.googlegroups.com>,
IJALAB wrote:
> hi all,
>
> i have a file in which there are 8 byte hex values one followed by
> another. For example:
> 2A414364
> 00001DA9
> 01A3F9DD
> 3FFFF661
> 00EE6670
> 000011CF
> 2BC43FD0
> 3FEB0003
> 3F7FFF40
>
> I need to check for the last bit in every data and if it being set,
> then the next data's 24 bits have to be inverted(assuming last bit is
> d31, starting from d26 to d7 needs to be inverted). For example:
> 1st data 0x2A414364 has last bit as '0' so the next word can be
> retained as same.But, 00001DA9(2nd data), the last bit is '1' so the
> next data 01A3F9DD will have to be printed as 3e5c061d. so, for the
> upper set of data, the below data needs to be printed. how do i do bit
> complement in hex using perl.
>
> 2a414364
> 00001da9
> 3e5c061d
> 000009a1
> 3f1199b0
> 000011cf
> 143bc010
> 3feb0003
> 00800080
You can complement bits by exclusive or'ing with a suitable bit mask.
Perl treats scalars as binary numbers in bitwise operations: &, |, ^.
I don't quite follow your bit numbering, but the following seems to do
what you want:
#!/usr/local/bin/perl
use strict;
use warnings;
my $invert;
my $data = 0x3fffffc0;
my $mask = $data ^ 0x7fffffff;
while() {
chomp;
my $n = hex($_);
if( $invert ) {
$n = ($n & $mask) | ( ($n & $data) ^ $data);
}
printf "%08x\n", $n;
$invert = $n & 1; # won't work if data includes last bit!
}
__DATA__
2A414364
00001DA9
01A3F9DD
3FFFF661
00EE6670
000011CF
2BC43FD0
3FEB0003
3F7FFF40
.... producing ...
2a414364
00001da9
3e5c061d
000009a1
3f1199b0
000011cf
143bc010
3feb0003
00800080
Note the warning on saving the last bit of each number. If your data
field includes bit 0 (leftmost), then you will have to save the bit in
an other variable.
--
Jim Gibson
Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com