www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - vrp bug?

reply Kagamin <spam here.lot> writes:
```
char BitsToChar(in uint bit5)
{
	const uint b=bit5&0x1f;
	return b<10?'0'+b:'a'+(b-10);
}
```
This code can't implicitly convert to char, but a version without 
parentheses `'a'+b-10` can. Is this intended or I miss something?
Jun 14
parent reply Forum User <forumuser example.com> writes:
On Sunday, 14 June 2026 at 09:06:13 UTC, Kagamin wrote:
 ```
 char BitsToChar(in uint bit5)
 {
 	const uint b=bit5&0x1f;
 	return b<10?'0'+b:'a'+(b-10);
 }
 ```
 This code can't implicitly convert to char, but a version 
 without parentheses `'a'+b-10` can. Is this intended or I miss 
 something?
``` (b - 10) ``` has type `uint` and wraps if `b` < 10. ``` 'a' + b - 10 ``` is evaluated left to right, `b` is < 32, so `'a' + b` is < 129. Substract 10 and the expression is < 119. It fits in the range of char. But the whole thing is questionable: - Drop the `const` in `b`'s declaration and the Error shows up in the unparenthesized version, too. - try `+127` and `+128` instead of `-10`.
Jun 14
parent Kagamin <spam here.lot> writes:
If b<10, `'0'+b` is returned.
Jun 15