google-code-export / gambas

Automatically exported from code.google.com/p/gambas
1 stars 0 forks source link

The bit manipulation functions does not work correctly on Shorts when negative values are returned #195

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
1) Describe the problem.
The bit manipulation functions at http://gambasdoc.org/help/cat/bit?v3 does not 
work correctly when negative Shorts are returned.
If, for example, the return value should be -1, it actually returns a Short 
with the value 65535 (which is outside the Short range!).

2) GIVE THE FOLLOWING INFORMATIONS (if they are appropriate):

Version: TRUNK
Revision: r4404

3) Provide a little project that reproduces the bug or the crash.
Print Asr(CShort(-4), 1) '=> 65534
Print BSet(CShort(-2), 0) '=> 65535
Print Ror(CShort(1), 1) '=> 32768
Print Shl(CShort(-1), 0) '=> 65535

And a little tip for the Shr/Asr implementation:
Are this kind of code really needed?
val = (((int)val >> bit) & 0x7FFFFFFF) | (((int)val) & 0x80000000);
Wouldn't it be enough with:
val = ((int)val >> bit);
since >> with signed datatypes in C already keeps the sign?

Original issue reported on code.google.com by emil.len...@gmail.com on 19 Jan 2012 at 12:25

GoogleCodeExporter commented 9 years ago

Original comment by benoit.m...@gmail.com on 19 Jan 2012 at 1:35

GoogleCodeExporter commented 9 years ago
It should be fixed in revision #4406.

As for your tip: apparently you are right, but I must check why I did that 
before - I don't remember.

Original comment by benoit.m...@gmail.com on 19 Jan 2012 at 1:36

GoogleCodeExporter commented 9 years ago

Original comment by benoit.m...@gmail.com on 21 Jan 2012 at 3:22

GoogleCodeExporter commented 9 years ago

Original comment by benoit.m...@gmail.com on 21 Jan 2012 at 3:25