www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Checked shift?

reply "bearophile" <bearophileHUGS lycos.com> writes:
Is it a good idea to add int/uint/long/ulong functions for the 
"<<" left shift operation here?

https://github.com/D-Programming-Language/druntime/blob/master/src/core/checkedint.d

Bye,
bearophile
Jul 31 2014
parent reply "H. S. Teoh via Digitalmars-d-learn" <digitalmars-d-learn puremagic.com> writes:
On Thu, Jul 31, 2014 at 05:00:22PM +0000, bearophile via Digitalmars-d-learn
wrote:
 Is it a good idea to add int/uint/long/ulong functions for the "<<"
 left shift operation here?
 
 https://github.com/D-Programming-Language/druntime/blob/master/src/core/checkedint.d
[...] What would you check for? Shifting something that already has its high bit set? T -- Don't drink and derive. Alcohol and algebra don't mix.
Jul 31 2014
parent reply "bearophile" <bearophileHUGS lycos.com> writes:
H. S. Teoh:

 What would you check for? Shifting something that already has 
 its high bit set?
If you have a uint where the 3 most significant bits are 1, and you shift it 3 bits on the left, you lose those three bits, you have an overflow. The point of checkedint functions/intrinsics is to reveal overflows. Bye, bearophile
Jul 31 2014
parent reply "H. S. Teoh via Digitalmars-d-learn" <digitalmars-d-learn puremagic.com> writes:
On Thu, Jul 31, 2014 at 06:08:56PM +0000, bearophile via Digitalmars-d-learn
wrote:
 H. S. Teoh:
 
What would you check for? Shifting something that already has its
high bit set?
If you have a uint where the 3 most significant bits are 1, and you shift it 3 bits on the left, you lose those three bits, you have an overflow. The point of checkedint functions/intrinsics is to reveal overflows.
[...] OK, makes sense. But what about if only the most significant bit is 1? Wouldn't that also be an overflow? So you're essentially checking if the topmost n bits are zero, where n is the number of bits you wish to shift by. T -- BREAKFAST.COM halted...Cereal Port Not Responding. -- YHL
Jul 31 2014
parent "bearophile" <bearophileHUGS lycos.com> writes:
H. S. Teoh:

 OK, makes sense. But what about if only the most significant 
 bit is 1?
 Wouldn't that also be an overflow? So you're essentially 
 checking if the
 topmost n bits are zero, where n is the number of bits you wish 
 to shift by.
Of course. https://issues.dlang.org/show_bug.cgi?id=13231 Bye, bearophile
Jul 31 2014