Solved

calulating parity

Posted on 2001-06-27
9
383 Views
Last Modified: 2010-04-21
hi ,

input = file with numerical lines :
example :

1000c5c548a0000650e21b42d04554374
0000809d1dc0141450ee139814b014398
1000aaaa41e8ce2050ed4a587eb554394
00003d8b4f01f1e9511d1358000094450
0000c94b1e402e7050da0f180b1c9435c

assumptions :
1. digits can be either decimal or hex'
2. lines may include spaces between fields
3. lines may be different long (in example all lines are 33 char's long)
4. no blank lines

required script :
get input_file + base (default base = hex)
output :
numbers of lines whose parity is odd
+ print line

* if you know perl , it can be also in perl .

Talmash

*** don't think its homework ... I am a hardware engineer , I need this script to debug a bus of data from an external SRAM to the CHIP we design .

0
Comment
Question by:Talmash
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
9 Comments
 
LVL 5

Expert Comment

by:marecs
ID: 6231796
Try this

#!/usr/bin/perl                                                                
                                                                               
use Math::BigInt;                                                              
                                                                               
($filename, $base) = @ARGV;                                                    
                                                                               
open IN, $filename or die $!;                                                  
@lines = <IN>;                                                                  
close IN;                                                                      
$oddlines = 0;                                                                  
                                                                               
chomp @lines;                                                                  
foreach $line (@lines) {                                                        
    if ($base == 10) { # base 10                                                
        $i = new Math::BigInt $line;                                            
        $bits = 0;                                                              
        while ($i != 0) {                                                      
             $bits += $i % 2;                                                  
             $i /= 2;                                                          
        }                                                                      
    } else { # base 16                                                          
        $bits = 0;                                                              
        foreach $byte (split // , $line) {                                      
            $line =~ /\s/ && next;                                              
            $bits += $_ foreach (split //, unpack("b4",pack("h", $byte)));      
        }                                                                      
    }                                                                          
    $parity = $bits % 2;                                                        
    if ($parity) {                                                              
        $oddlines++;                                                            
        print "$line\n";                                                        
    }                                                                          
}                                                                              
                                                                               
print "number of odd parity lines = $oddlines\n";                              
 
0
 
LVL 5

Expert Comment

by:marecs
ID: 6231820
two errors.
chomp @lines;
after close IN;

$byte =~ /\s/ && next;
instead of $line =~
0
 
LVL 6

Author Comment

by:Talmash
ID: 6231934
hi marecs

tnx for the work .
I run your suggestion , with the corrections .

result :
18:21{184}bin> par_check tmp
par_check: Command not found.

I changed 1st line to :
#!/usr/local/bin/perl5 -wx

and got this result :
18:21{183}bin> par_check tmp
syntax error at par_check line 35, near "$_ foreach "
Execution of par_check aborted due to compilation errors.

have you checked the script at your machine , and it worked?

any way , I rise points for your efforts .

be sure grade will be A .

Talmash
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 5

Expert Comment

by:marecs
ID: 6231973
Talmash,

Yes, it definitely worked on my machine.It may not be bug free, because I only tried a few simple lines that I know the parity of.

Try this instead of line 35

foreach (split //, unpack("b4",pack("h", $byte))) {
    $bits += $_;
}

0
 
LVL 6

Author Comment

by:Talmash
ID: 6234067
EXCELLENT !

great job .

I may be too excited , but it works just fine .

I used lines with spaces ,

short and long lines together .

BUT , base 10 doesn't work properely .

try this example :

100  
064372 56783537634 0349361354384
00009  659256336 4776330328000094418
100003392440203476430343772614343
000033333931419976495558148314480
100093834420000676440998054414444

command line : par_check in_file 10

Talmash
0
 
LVL 5

Expert Comment

by:marecs
ID: 6234364
Talmash,

I am not clear about a few things.

With
100003392440203476430343772614343
do you mean the 107 bit number
10011101110001110000110001101101010100000110111010000111000011001111000001000111000000101001010111011000111
And you want to know the parity of this number? This particular number has even parity?

064372 56783537634 0349361354384 actually has 3 numbers in the line, and the parity of each number must be calculated seperately? Do you then want the number of fields who's parity is odd, rather than the number of lines?
0
 
LVL 5

Accepted Solution

by:
marecs earned 200 total points
ID: 6235380
This assumes that the answers to my previous post are true.

#!/usr/bin/perl -w

use Math::BigInt;

($filename, $base) = @ARGV;
defined($base) or $base = 16;

open IN, $filename or die $!;
@lines = ();
$/ = undef;
push @lines , split /\s+/, <IN>;
close IN;

$oddlines = 0;

foreach $line (@lines) {
    if ($base == 10) { # base 10
        $i = new Math::BigInt $line;
        $bits = 0;
        while ($i != 0) {
             $bits += $i % 2;
             $i /= 2;
        }
    } else { # base 16
        $bits = 0;
        foreach $byte (split // , $line) {
            foreach (split //, unpack("b4",pack("h", $byte))) {
                $bits += $_;
            }
        }
    }
    $parity = $bits % 2;
    if ($parity) {
        $oddlines++;
        print "$line\n";
    }
}

print "number of odd parity fields = $oddlines\n";
0
 
LVL 84

Expert Comment

by:ozo
ID: 6235959
#an easier way to calculate parity for base 16 is
$parity ^= unpack"%1b*",pack"h*", $line;

0
 
LVL 6

Author Comment

by:Talmash
ID: 6241920
hi ,

as I said before , you deserve it , for your quick response.
about decimal lines : It is low priority by now , so lets
forget our little bug.


as to ozo , I'll check your proposed line .

tnx all ,

question is closed .

Tal
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Problem Imaging Computers With Clonezilla 2 104
How to clone solaris 10 machine 33 194
unix solaris snoop command 6 139
Adding a continue option in a script 9 75
In tuning file systems on the Solaris Operating System, changing some parameters of a file system usually destroys the data on it. For instance, changing the cache segment block size in the volume of a T3 requires that you delete the existing volu…
FreeBSD on EC2 FreeBSD (https://www.freebsd.org) is a robust Unix-like operating system that has been around for many years. FreeBSD is available on Amazon EC2 through Amazon Machine Images (AMIs) provided by FreeBSD developer and security office…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question