On Tue, Sep 16, 2008 at 7:07 PM, John W. Krahn <[EMAIL PROTECTED]> wrote:
> Amit Saxena wrote:
>
>> On Tue, Sep 16, 2008 at 5:04 PM, John W. Krahn <[EMAIL PROTECTED]> wrote:
>>
>>>
>>> Amit Saxena wrote:
>>>
>>>>
>>>> In the following code, the value of "$string" in last two cases is not
>>>> printed correctly.
>>>>
>>>> Please let me know what i am missing over here.
>>>>
>>>> *# cat l3.pl*
>>>> #! /usr/bin/perl
>>>>
>>>>
>>>> $ONE_BYTE_RANGE = 256;
>>>>
>>>
>>> A one byte range is 0 to 255 so 256 is 1 bit more than one byte.
>>>
>>> $TWO_BYTE_RANGE = 65536;
>>>>
>>> Same here, you are one bit over the two byte range.
>>>
>>> $THREE_BYTE_RANGE = 4294967296;
>>>> $THREE_BYTE_RANGE_1 = 4294967295;
>>>>
>>>
>>> A three byte range is 0 to 16777215. What you have there is a _four_
>>> byte
>>> range plus one.
>>>
>>> If you have a 32 bit computer then the largest integer that perl can use
>>> is
>>> 4294967295.
>>>
>>> $string = sprintf( "%d, %d ", hex( "9A" ), $ONE_BYTE_RANGE );
>>>> print (" String = $string \n ");
>>>> $string = sprintf( "%d", hex( "9A" ) - $ONE_BYTE_RANGE );
>>>> print (" String = $string \n ");
>>>> $string = sprintf( "%d , %d", hex( "BB76" ), $TWO_BYTE_RANGE );
>>>> print (" String = $string \n ");
>>>> $string = sprintf( "%d", hex( "BB76" ) - $TWO_BYTE_RANGE );
>>>> print (" String = $string \n ");
>>>> $string = sprintf( "%ld , %ld ", hex("98EAB"), $THREE_BYTE_RANGE );
>>>> print (" String = $string \n ");
>>>> $string = sprintf( "%ld", hex("98EAB") - $THREE_BYTE_RANGE ) ;
>>>> print (" String = $string \n ");
>>>> $string = sprintf( "%ld", (hex("98EAB") - $THREE_BYTE_RANGE_1 - 1) ) ;
>>>> print (" String = $string \n ");
>>>>
>>>> *# perl l3.pl*
>>>> String = 154, 256
>>>> String = -102
>>>> String = 47990 , 65536
>>>> String = -17546
>>>> String = 626347 , -1
>>>> String = -2147483648
>>>> String = -2147483648
>>>>
>>>
>>> What numbers did you expect would be printed?
>>>
>>
>> What my doubt is why the output of following two statements is same :-
>>
>> $string = sprintf( "%ld", hex("98EAB") - $THREE_BYTE_RANGE ) ;
>> print (" String = $string \n ");
>> $string = sprintf( "%ld", (hex("98EAB") - $THREE_BYTE_RANGE_1 - 1) ) ;
>> print (" String = $string \n ");
>>
>
> Because the expression "hex("98EAB") - $THREE_BYTE_RANGE_1 - 1" is
> evaluated as "( hex("98EAB") - $THREE_BYTE_RANGE_1 ) - 1" and not as
> "hex("98EAB") - ( $THREE_BYTE_RANGE_1 - 1 )" as you probably intended, so it
> appears that subtracting 1 from a number that has overflowed/underflowed is
> not affecting the total. (This may be a bug in Perl, I don't know.)
>
> $ perl -le'
> $THREE_BYTE_RANGE = 4_294_967_296;
> $THREE_BYTE_RANGE_1 = 4_294_967_295;
> print join " ", hex( "98EAB" ), $THREE_BYTE_RANGE, hex( "98EAB" ) -
> $THREE_BYTE_RANGE;
> print join " ", hex( "98EAB" ), $THREE_BYTE_RANGE_1 - 1, ( hex( "98EAB" )
> - $THREE_BYTE_RANGE_1 ) - 1;
> print join " ", hex( "98EAB" ), $THREE_BYTE_RANGE_1 - 1, hex( "98EAB" ) -
> ( $THREE_BYTE_RANGE_1 - 1 );
> '
> 626347 4294967296 -4294340949
> 626347 4294967294 -4294340949
> 626347 4294967294 -4294340947
>
>
> That also raises another question, does the perl also has a concept of
>> signed integer representation like "C".
>>
>
> The pack()/unpack()/printf()/sprintf() formats are based on C data types
> and have signed and unsigned representations.
>
>
>
>
> John
> --
> Perl isn't a toolbox, but a small machine shop where you
> can special-order certain sorts of tools at low cost and
> in short order. -- Larry Wall
>
> --
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
> http://learn.perl.org/
>
>
>
Are you working on 64 bit system ?
Regards,
Amit Saxena