Re: Odd Perl bitwise-AND & MySQL problem?

Discussion in 'Perl' started by dohnut, Oct 21, 2003.

  1. dohnut

    dohnut Guest

    Here's some working code that I created to show the problem.

    First the MySQL database dump
    -------------------------------------------------------------
    CREATE DATABASE /*!32312 IF NOT EXISTS*/ pbug;

    USE pbug;

    CREATE TABLE hosts (
    id int(11) NOT NULL auto_increment,
    ip int(4) unsigned default NULL,
    password varchar(32) default NULL,
    description varchar(255) default '',
    clone int(4) unsigned default '0',
    conn int(4) unsigned default '0',
    status int(4) unsigned default '0',
    tdate timestamp(14) NOT NULL,
    alarms int(11) unsigned default '0',
    monitored int(11) unsigned default '0',
    installed int(11) unsigned default '0',
    soft_ver_major int(4) default '0',
    soft_ver_minor int(4) default '0',
    soft_ver_incr int(4) default '0',
    hard_ver_major int(4) default '0',
    hard_ver_minor int(4) default '0',
    hard_ver_incr int(4) default '0',
    case_temp int(4) default '-1',
    cpu1_temp int(4) default '-1',
    cpu2_temp int(4) default '-1',
    ext_temp int(4) default '-1',
    PRIMARY KEY (id),
    UNIQUE KEY ip (ip)
    ) TYPE=MyISAM;


    INSERT INTO hosts VALUES (1,168430090,'crystal','Demo
    XXXXXXXX',0,129,0,20031020210116,0,61,7,0,0,0,0,0,0,29,-1,-1,-1);
    INSERT INTO hosts VALUES (2,185273099,'password','Test XXXXXXXX
    #1\r\n',0,129,0,20031020210117,57,2045,2047,0,0,0,0,0,0,31,-1,-1,-1);
    INSERT INTO hosts VALUES (3,202116108,'password','Another
    XXXXXXXX\r\n\r\n',0,129,0,20031020210118,3,7,63,0,0,0,0,0,0,30,-1,-1,-1);
    --------------------------------------------

    Put this into a file and add it to your mysql server.
    type: mysql -u <username> -p<password> < pbug.sql

    Ok, and here's my little perl module that demonstrates the error (or
    my stupidity).
    ------------------------------------------------
    #!/usr/bin/perl -w

    use strict;
    use warnings;
    use DBI;


    foreach (1..3)
    {
    my @dat = &GetHostStatus( $_ );

    print "$_: ";
    #printf "0x%x & 0x%x & 0x%x & 0x7fff = ", $dat[3], $dat[4],
    $dat[5];
    print $dat[3] & $dat[4] & $dat[5] & 0x7fff;
    print "\n";
    }

    1;

    ########################################################################

    sub GetHostStatus()
    {
    my $id = shift || return ();

    my $db = DBI->connect( 'dbi:mysql:pbug','bla','bla' ) || die(
    DBI->errstr );

    my $query = "SELECT ip, password, description, alarms, monitored,
    installed, case_temp, cpu1_temp, cpu2_temp, ext_temp FROM hosts WHERE
    id = $id";

    my $sth = $db->prepare( $query ) || die( DBI->errstr );

    $sth->execute() || die( DBI->errstr );

    my @row = $sth->fetchrow_array();

    $sth->finish;
    $db->disconnect;

    return @row;
    }
    --------------------------------------------------

    When I run this on my system, here is my output:

    1: 0
    2: 0
    3: 2

    Clearly incorrect. Now, when I uncomment that printf line:

    1: 0x0 & 0x3d & 0x7 & 0x7fff = 0
    2: 0x39 & 0x7fd & 0x7ff & 0x7fff = 57
    3: 0x3 & 0x7 & 0x3f & 0x7fff = 3

    Then it works.. Hmm...

    I've tested this on 2 separate systems now with the same results, I'd
    be interested to see if any of you guys can duplicate it also? Or
    tell me where I screwed up, that would be better (and more likely).

    Regardless, I'd appreciate any feedback.

    - Chris
    dohnut, Oct 21, 2003
    #1
    1. Advertising

  2. dohnut

    Sam Holden Guest

    On 20 Oct 2003 20:16:05 -0700, dohnut <> wrote:
    > Here's some working code that I created to show the problem.
    >
    > First the MySQL database dump

    [snip the dump]
    > ------------------------------------------------
    > #!/usr/bin/perl -w
    >
    > use strict;
    > use warnings;
    > use DBI;
    >
    >
    > foreach (1..3)
    > {
    > my @dat = &GetHostStatus( $_ );


    Why are you using the & there?

    >
    > print "$_: ";
    > #printf "0x%x & 0x%x & 0x%x & 0x7fff = ", $dat[3], $dat[4],
    > $dat[5];
    > print $dat[3] & $dat[4] & $dat[5] & 0x7fff;
    > print "\n";
    > }


    [snip the GetHostStatus code]

    > --------------------------------------------------
    >
    > When I run this on my system, here is my output:
    >
    > 1: 0
    > 2: 0
    > 3: 2
    >
    > Clearly incorrect. Now, when I uncomment that printf line:
    >
    > 1: 0x0 & 0x3d & 0x7 & 0x7fff = 0
    > 2: 0x39 & 0x7fd & 0x7ff & 0x7fff = 57
    > 3: 0x3 & 0x7 & 0x3f & 0x7fff = 3
    >
    > Then it works.. Hmm...


    The values are strings. Perl defines a bitwise & operator for
    strings and hence uses it. Doing the printf causes perl to generate
    a numeric version of the value, so then the & operator operates
    on the numbers.

    The database stuff is all irrelevant (aside from the fact it is returning
    strings):

    # start with numbers
    my ($x,$y,$z) = (0x39,0x7fd,0x77f);
    print $x & $y & $z & 0x7fff, "\n";

    # create string variables containing the numbers as strings.
    my ($sx, $sy, $sz) = ("$x", "$y", "$z");
    print $sx & $sy & $sz & 0x7fff, "\n";

    # create numeric 'version's of the strings...
    # adding 0 seems clearer than the printf side effect :)
    $_ += 0 for ($sx, $sy, $sz);
    print $sx & $sy & $sz & 0x7fff, "\n";

    Things like this is why the knowledgable people here always argue against
    posters who quote variables for no reason (replace the '("$x", "$y", "$z")'
    in that code with '($z, $y, $z)' and the bahaviour will change).

    --
    Sam Holden
    Sam Holden, Oct 21, 2003
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. dohnut
    Replies:
    0
    Views:
    531
    dohnut
    Oct 20, 2003
  2. dohnut
    Replies:
    0
    Views:
    575
    dohnut
    Oct 21, 2003
  3. Serve Laurijssen

    odd/even bitwise and

    Serve Laurijssen, Apr 6, 2004, in forum: C Programming
    Replies:
    27
    Views:
    1,203
    Michael Wojcik
    Apr 13, 2004
  4. Michael Speer

    Odd behavior with odd code

    Michael Speer, Feb 16, 2007, in forum: C Programming
    Replies:
    33
    Views:
    1,062
    Richard Heathfield
    Feb 18, 2007
  5. dohnut
    Replies:
    7
    Views:
    124
    Tad McClellan
    Oct 21, 2003
Loading...

Share This Page