## digitalmars.D.learn - BigInt and xor

• Dennis Ritchie (15/15) Mar 24 2015 Tell me, please, how can I replace this code?
• Rene Zwanenburg (5/20) Mar 24 2015 Looks right to me. What output would you expect?
• matovitch (4/19) Mar 24 2015 Hi,
• Ivan Kazmenko (28/43) Mar 24 2015 What exactly is not working?
• Dennis Ritchie (9/35) Mar 24 2015 Everything works. I'm just a little forgotten properties of the
• matovitch (3/10) Mar 24 2015 xor it with -1 instead of 1. (-1 is store as 0xfff..f with the
"Dennis Ritchie" <dennis.ritchie mail.ru> writes:
```Tell me, please, how can I replace this code?

import std.conv : to;
import std.bigint : BigInt;
import std.string : format;
import std.stdio : writeln;

void main() {

BigInt[10] bitArr;

ulong n = 18_446_724_073_709_551_614U;

bitArr[0] = format("%b", n).to!BigInt;

writeln(bitArr[0]);
writeln(bitArr[0] ^ 1); // not work

}

Output:
1111111111111111111011011100111101100011000110101011111111111110
1111111111111111111011011100111101100011000110101011111111111111
```
Mar 24 2015
"Rene Zwanenburg" <renezwanenburg gmail.com> writes:
```On Tuesday, 24 March 2015 at 15:45:36 UTC, Dennis Ritchie wrote:
Tell me, please, how can I replace this code?

import std.conv : to;
import std.bigint : BigInt;
import std.string : format;
import std.stdio : writeln;

void main() {

BigInt[10] bitArr;

ulong n = 18_446_724_073_709_551_614U;

bitArr[0] = format("%b", n).to!BigInt;

writeln(bitArr[0]);
writeln(bitArr[0] ^ 1); // not work

}

Output:
1111111111111111111011011100111101100011000110101011111111111110
1111111111111111111011011100111101100011000110101011111111111111

Looks right to me. What output would you expect?

Also, if you need a bit array you can simply use std.container's
Array!bool. It's specialized for bool and uses only one bit per
element.
```
Mar 24 2015
"matovitch" <camille.brugel laposte.net> writes:
```On Tuesday, 24 March 2015 at 15:45:36 UTC, Dennis Ritchie wrote:
Tell me, please, how can I replace this code?

import std.conv : to;
import std.bigint : BigInt;
import std.string : format;
import std.stdio : writeln;

void main() {

BigInt[10] bitArr;

ulong n = 18_446_724_073_709_551_614U;

bitArr[0] = format("%b", n).to!BigInt;

writeln(bitArr[0]);
writeln(bitArr[0] ^ 1); // not work

}

Output:
1111111111111111111011011100111101100011000110101011111111111110
1111111111111111111011011100111101100011000110101011111111111111

Hi,

Well it works, the las bit is flip isn't it ? What are you trying
to achieve ?
```
Mar 24 2015
"Ivan Kazmenko" <gassa mail.ru> writes:
```On Tuesday, 24 March 2015 at 15:45:36 UTC, Dennis Ritchie wrote:
Tell me, please, how can I replace this code?

import std.conv : to;
import std.bigint : BigInt;
import std.string : format;
import std.stdio : writeln;

void main() {

BigInt[10] bitArr;

ulong n = 18_446_724_073_709_551_614U;

bitArr[0] = format("%b", n).to!BigInt;

writeln(bitArr[0]);
writeln(bitArr[0] ^ 1); // not work

}

Output:
1111111111111111111011011100111101100011000110101011111111111110
1111111111111111111011011100111101100011000110101011111111111111

What exactly is not working?
The only thing I see lacking is an ability to print a BigInt in
binary via writefln("%b").

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

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

Ivan Kazmenko.
```
Mar 24 2015
"Dennis Ritchie" <dennis.ritchie mail.ru> writes:
```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.
```
Mar 24 2015
"matovitch" <camille.brugel laposte.net> writes:
```On Tuesday, 24 March 2015 at 17:28:50 UTC, Dennis Ritchie wrote:
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.

xor it with -1 instead of 1. (-1 is store as 0xfff..f with the
classic modular arithmetic)
```
Mar 24 2015
"Dennis Ritchie" <dennis.ritchie mail.ru> writes:
```On Tuesday, 24 March 2015 at 17:35:14 UTC, matovitch wrote:
xor it with -1 instead of 1. (-1 is store as 0xfff..f with the
classic modular arithmetic)

Thanks.
```
Mar 24 2015