www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - why arithmetic for bytes different than for ints?

reply "Vlad Levenfeld" <vlevenfeld gmail.com> writes:
Can anyone explain to me why this test passes? I have read 
through the Types page on the dlang website and can't find 
anything that would explain this. Is it a bug or am I doing it 
wrong?

	uint x = 0;
	(x += 1) %= 2;
	assert (x == 1);
	ubyte y = 0;
	(y += 1) %= 2;
	assert (y != 1);
	// though I expected y == 1

Here's a table of all the types I tried ++x %= 2, where x has 
type long (L), int (I), etc:
	L	I	S	B	uL	uI	uS	uB
	1	1	0	0	1	1	0	0
	0	0	0	0	0	0	0	0
	1	1	0	0	1	1	0	0
	0	0	0	0	0	0	0	0

It all seems very odd.
May 02 2014
parent reply "Vlad Levenfeld" <vlevenfeld gmail.com> writes:
Dunno why I didn't try it earlier, but splitting it across lines 
like

y += 1;
y %= 2;
assert (y == 1);

works. So, bug, then?
May 02 2014
parent "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Fri, 02 May 2014 16:32:54 -0400, Vlad Levenfeld <vlevenfeld gmail.com>  
wrote:

 Dunno why I didn't try it earlier, but splitting it across lines like

 y += 1;
 y %= 2;
 assert (y == 1);

 works. So, bug, then?
Yes. -Steve
May 02 2014