-
Notifications
You must be signed in to change notification settings - Fork 0
/
bit_positions.pl
54 lines (48 loc) · 1.27 KB
/
bit_positions.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
####################################################
# script name : bit_position.pl
# description : compare 2 bit value at 2 positions of a number
# author : Dai Nguyen-Van
# email : dainv1989@gmail.com
# creation date : 11/10/2013
####################################################
#
#!/usr/bin/perl
# function : dec2bin
# usage : convert a decimal number to binary (32 bits Big-endian)
sub dec2bin
{
# convert decimal number to binary
my $str = unpack("B32", pack("N", shift));
return $str;
}
# open file in read-only mode
# read content of input file
# remove all end of line character (EOL)
open (DATA, "<", $ARGV[0]) or die $!;
@content = <DATA>;
chomp(@content);
foreach (@content)
{
# get parameters
@tmp = split(",",$_);
# check validation of line
# valid condition: not empty and contains 3 parameters
# if invalid, continue handling next line
if (($_ eq "") || ($#tmp != 2))
{
next;
}
# convert a number to binary
# then split each bit, reverse right order and assign to an array
@bits = reverse split('', &dec2bin($tmp[0]));
if ($bits[$tmp[1] - 1] == $bits[$tmp[2] - 1])
{
print "true\n";
}
else
{
print "false\n";
}
}
# close file handle
close DATA;