constraints in perl

constraints in perl

am 01.10.2007 16:10:50 von 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

Re: constraints in perl

am 01.10.2007 16:32:11 von Ian Wilson

Amir wrote:
> 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

Re: constraints in perl

am 01.10.2007 16:33:49 von Mirco Wahab

Amir wrote:
> 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.

Re: constraints in perl

am 01.10.2007 16:35:43 von Amir

On Oct 1, 4:32 pm, Ian Wilson wrote:
> Amir wrote:
> > 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...

Re: constraints in perl

am 01.10.2007 16:59:31 von Mirco Wahab

Amir wrote:
> 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.

Re: constraints in perl

am 01.10.2007 17:19:15 von jurgenex

Amir wrote:
> 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

Re: constraints in perl

am 01.10.2007 17:31:56 von Peter Makholm

Amir writes:

> 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

Re: constraints in perl

am 01.10.2007 19:13:11 von Mahesh M

On Oct 1, 7:10 am, Amir wrote:
> 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.