www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Strange behaviour trying to bitwise or two negative ints into a long

reply Deewiant <deewiant.doesnotlike.spam gmail.com> writes:
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.
May 11 2007
parent reply Thomas Kuehne <thomas-dloop kuehne.cn> writes:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Deewiant schrieb am 2007-05-11:
 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.
There is a bug as well as a misunderstanding. time accepting \"sizeof(x) * 8\" is a bug"); Thomas -----BEGIN PGP SIGNATURE----- iD8DBQFGRPj7LK5blCcjpWoRAllcAJ9Hk6eFxiqmxcrpLmjeFwqSy0+P9ACbBWUR zWVUdAXJdn1VJFkdf5B7q4o= =if/F -----END PGP SIGNATURE-----
May 11 2007
parent Deewiant <deewiant.doesnotlike.spam gmail.com> writes:
Thomas Kuehne wrote:
 Deewiant schrieb am 2007-05-11:
 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.
There is a bug as well as a misunderstanding. time accepting \"sizeof(x) * 8\" is a bug");
Thanks for this, that clarifies it for me. Added a note to Bug 550. -- Remove ".doesnotlike.spam" from the mail address.
May 12 2007