BigInt and xor

Dennis Ritchie via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Mar 24 10:28:48 PDT 2015


On Tuesday, 24 March 2015 at 16:35:04 UTC, Ivan Kazmenko wrote:
> What exactly is not working?

Everything works. I'm just a little forgotten properties of the 
operation xor.

I just wanted to xor 1 each digit in the number of type BigInt, 
while I would like to store each number in the binary 
representation of the array BigInt.

> The only thing I see lacking is an ability to print a BigInt in 
> binary via writefln("%b").

Yes. It would be nice.

> Up to 64 bits, arithmetic and bitwise operations, including 
> xor, are available with long and ulong. Just print the result 
> as binary:
>
> -----
> import std.stdio;
> void main() {
> 	ulong n = ulong.max - 0b1000101;
> 	writeln (n);
> 	writefln ("%b", n);
> 	writefln ("%b", n ^ 1);
> }
> -----
> Output:
> -----
> 18446744073709551546
> 1111111111111111111111111111111111111111111111111111111110111010
> 1111111111111111111111111111111111111111111111111111111110111011
> -----

> If you need more than 64 bits, take a look at BitArray here, it 
> also has xor defined:
> http://dlang.org/phobos/std_bitmanip.html#.BitArray

Thanks.

> In the future, please explain what problem you are trying to 
> solve, as the wrong code alone often leaves one guessing.

OK, I will try.


More information about the Digitalmars-d-learn mailing list