Strange behaviour trying to bitwise or two negative ints into a long

Deewiant deewiant.doesnotlike.spam at gmail.com
Fri May 11 12:18:56 PDT 2007


import std.stdio;

int a = -1;
int b = -2;

void main() {
	writefln("       a: 0b%064b", a);
	writefln("       b: 0b%064b", cast(long)b);
	writefln(" b << 32: 0b%064b", cast(long)b << 32);
	writefln("| a     : 0b%064b", cast(long)b << 32 | a);
	writefln("| a &   : 0b%064b", cast(long)b << 32 | (a & 0xffff_ffff));
	writefln("| cast a: 0b%064b", cast(long)b << 32 | cast(uint)a);

	/+ using values copied from the outputs of the first two writeflns above +/
	writefln("binconst: 0b%064b",
0b1111111111111111111111111111111111111111111111111111111111111110 << 32 |
0b0000000000000000000000000000000011111111111111111111111111111111);
}

Why do the last writeflns output a different number than the plain cast(long)b
<< 32 | a one? Why do I need the cast or bitwise and?

I was originally going to file a bug but I think I'm just misunderstanding
something fundamental.

-- 
Remove ".doesnotlike.spam" from the mail address.


More information about the Digitalmars-d-learn mailing list