constraints in perl

A

Amir

hi,
I have a small question
if I have a variable called $foo , and I want to check that this
variable "bits" 20-23 and 31 is equal to other variable $too "bits"
12-14 and 29,respectively.
I mean $foo[20]=$too[12] and $foo[21]=$too[13] and $foo[22] =$too[14]
and $foo[31] = $too[29] .
do you have any ideas?!
I thanks you in advance
-Amir
 
I

Ian Wilson

Amir said:
hi,
I have a small question
if I have a variable called $foo , and I want to check that this
variable "bits" 20-23 and 31 is equal to other variable $too "bits"
12-14 and 29,respectively.
I mean $foo[20]=$too[12] and $foo[21]=$too[13] and $foo[22] =$too[14]
and $foo[31] = $too[29] .
do you have any ideas?!

I'd look at "bitwise operators" in `perldoc perlop`. Use of shift and
bitwise 'and' should get you to the point where you can compare values.

perl -e 'print 12>>2==3?"true\n":"false\n"'
true

perl -e '$x=4;$y=($x&12)>>2;print "$y\n"'
1
 
M

Mirco Wahab

Amir said:
hi,
I have a small question
if I have a variable called $foo , and I want to check that this
variable "bits" 20-23 and 31 is equal to other variable $too "bits"
12-14 and 29,respectively.
I mean $foo[20]=$too[12] and $foo[21]=$too[13] and $foo[22] =$too[14]
and $foo[31] = $too[29] .
do you have any ideas?!

Perl has almost the same bitwise operators as
C/C++ (and others) has/have, therefore you'd
write the same kind, eg.:

...
my ($foo,$too) = (0x12345678, 0x87654321);
my $mask = (7 << 20) | (1 << 31);
my $both = $foo & $too & $mask;

printf "mask:\t%032b\nfoo:\t%032b\ntoo:\t%032b\n", $mask, $foo, $too;
printf "foo&too&mask:\n\t%032b (%d)\n", $both, $both;
...


Regards

M.
 
A

Amir

Amir said:
hi,
I have a small question
if I have a variable called $foo , and I want to check that this
variable "bits" 20-23 and 31 is equal to other variable $too "bits"
12-14 and 29,respectively.
I mean $foo[20]=$too[12] and $foo[21]=$too[13] and $foo[22] =$too[14]
and $foo[31] = $too[29] .
do you have any ideas?!

I'd look at "bitwise operators" in `perldoc perlop`. Use of shift and
bitwise 'and' should get you to the point where you can compare values.

perl -e 'print 12>>2==3?"true\n":"false\n"'
true

perl -e '$x=4;$y=($x&12)>>2;print "$y\n"'
1

I was looking at bitwise and shift operators, but the problem is the
Constraint solver that I'm working with becomes very 'heavy' when I
use shift operator, hence, I'm looking for alternative way to solve
this...
 
M

Mirco Wahab

Amir said:
I was looking at bitwise and shift operators, but the problem is the
Constraint solver that I'm working with becomes very 'heavy' when I
use shift operator, hence, I'm looking for alternative way to solve
this...

Can you explain why? Couldn't you
just compare the characters from
the bitwise representation, like:

...
my $f1 = join '', (split //, sprintf "%032b", $foo)[20..23, 31];
my $t1 = join '', (split //, sprintf "%032b", $too)[12..15, 29];

if($f1 eq $t1) {
print "bit are equal: $f1"
}
else {
print "bits are not equal $f1; $t1"
}
...

Regards

M.
 
J

Jürgen Exner

Amir said:
if I have a variable called $foo ,

A variable in Perl, even a scalar variable like $foo, can and does have many
different 'values'. Are you talking about a string, a natural number, a
floating point number, ...
and I want to check that this
variable "bits" 20-23 and 31 is equal to other variable $too "bits"
12-14 and 29,respectively.

The internal representation of a variable's value is subject to the compiler
and can change at any time and will most likely be different for different
platforms.
Relying on 'this bit of information is stored in bit 31' is, well, naive at
best.

Having said that Perl does have the usual bit-wise operators, see 'perldoc
perlop' for details.

jue
 
P

Peter Makholm

Amir said:
I have a small question
if I have a variable called $foo , and I want to check that this
variable "bits" 20-23 and 31 is equal to other variable $too "bits"
12-14 and 29,respectively.

You might have some luck using the vec() function. Probaly something
like:

vec($foo,5,4) == vec($too,4,4) && vec($foo,31,1) == vec($too,29,1)

Not that I have used the vec function a lot myself though. But read
the documentation and see if you can get it to work as you need.

//Makholm
 
M

Mahesh Asolkar

hi,
I have a small question
if I have a variable called $foo , and I want to check that this
variable "bits" 20-23 and 31 is equal to other variable $too "bits"
12-14 and 29,respectively.
I mean $foo[20]=$too[12] and $foo[21]=$too[13] and $foo[22] =$too[14]
and $foo[31] = $too[29] .
do you have any ideas?!

This may be an overkill for the one task you mention, but if you have
a lot of bit vector arithmetic going on, Bit::Vector module may be
handy.

http://search.cpan.org/dist/Bit-Vector/Vector.pod

For example:

#!/usr/bin/perl

use strict;
use warnings;

use Bit::Vector;

my $foo = Bit::Vector->new_Bin (5, '10101');
my $too = Bit::Vector->new_Bin (5, '11101');

foreach my $bit (0..4) {
print "Bit $bit is " .
(($foo->bit_test ($bit) == $too->bit_test($bit))
? "same" : "different") . "\n";
}
__END__

HTH,
Mahesh.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top