XOR Multiplication - The Daily Programmer #315

The Daily Program: Understanding XOR Multiplication

In this episode of The Daily Program, we're going to explore problem number 315 from the daily program and subreddit. For those who may not be familiar with the concept, multiplication using bitwise operations can seem daunting at first, but it's actually quite straightforward once you understand the basics.

To start, we need to break down the input numbers into their binary representations. In this case, we have two integers: 14 and 13. The binary representation of 14 is 1 1 1 0, while the binary representation of 13 is 1 1 0 1. Now that we have these representations, we can understand how to perform XOR multiplication. The algorithm states that for every bit in B starting from the right, you need to write out A and then indent for every bit if it's a 1.

Let's walk through this process step by step. Starting with the bottom bit of B (the one on the far right), we need to write out A if the bit is 1. In this case, the first bit of A is a 1, so we'll go ahead and write it out as 1 1 1 0. We've now taken care of that bit, so we can cross it off. Moving on to the next part of the multiplication, we indent 1 for every bit if it's a 1. So, in this case, we'll add another 1 to the end of A, making it 1 1 1 1 0.

Now that we have our starting points, let's look at how we perform XOR multiplication. The process involves taking the result and keeping XORing it with A if the rightmost bit of B is equal to 1. This might seem a bit confusing at first, but once you understand what's going on, it makes sense.

To demonstrate this, let's consider an example. Suppose we have B = 1 1 0 1 and A = 1 1 1 0. We'll start by taking the result and keeping XORing it with A if the rightmost bit of B is equal to 1. In this case, the first iteration will print out 1 1 1 0 because we're multiplying 1 1 1 0 by 1 (the most right bit of B). The next iteration will print out 1 1 1 1 0 again because we're still multiplying 1 1 1 0 by 1. We continue this process until the loop reaches 0.

The key to understanding XOR multiplication is recognizing that each iteration represents a step in the multiplication process. By shifting A over to the left one position and adding a zero, we effectively multiply A by B. This might seem like a complex concept at first, but once you understand how it works, it's actually quite straightforward.

To further demonstrate this, let's look at the code used for the XOR multiplication algorithm. The code uses bitwise operations to perform the multiplication process. Specifically, it involves shifting A over to the left one position and adding a zero. This is achieved using the less than or equal operator (&=) in C, which pushes the integer A of its binary representation over one.

The final step in the XOR multiplication algorithm is to take the result and keep XORing it with A if the rightmost bit of B is equal to 1. To demonstrate this, let's consider an example again. Suppose we have B = 1 1 0 1 and A = 1 1 1 0. We'll start by taking the result and keeping XORing it with A if the rightmost bit of B is equal to 1.

For the first iteration, we'll print out 1 1 1 0 because we're multiplying 1 1 1 0 by 1 (the most right bit of B). The next iteration will print out 1 1 1 0 again because we're still multiplying 1 1 1 0 by 1. We continue this process until the loop reaches 0.

By examining the code and understanding how XOR multiplication works, you can see that it's actually quite straightforward once you understand the basics. The key is recognizing how each iteration represents a step in the multiplication process and how we use bitwise operations to perform the calculation.

The Code

To demonstrate the XOR multiplication algorithm in action, let's look at the code used for this purpose. The code uses the following variables:

* `B` The input number B

* `A` The input number A

* `result` The result of the XOR multiplication

The code begins by initializing `B` and `A` with their respective binary representations.

```c

int B = 1; // Most right bit is on

int A = 1; // Most right bit is on

```

Next, we shift `B` over to the left one position using bitwise operations. This effectively multiplies `A` by `B`. We do this using the following code:

```c

// Shift B over to the left one position

for (int i = 0; i < 3; i++) {

B >>= 1;

}

```

We continue this process until we reach 0, effectively performing the XOR multiplication. We also keep track of the result and update it accordingly.

```c

// Perform XOR multiplication

int result = A;

for (int i = 0; i < 3; i++) {

if (B & 1) { // Check if rightmost bit of B is equal to 1

result ^= A; // XOR result with A

}

A <<= 1; // Shift A over to the left one position

B >>= 1; // Shift B over to the left one position

}

```

Finally, we print out the final result using bitwise operations.

```c

// Print final result

console.log("Final Result: ", result);

```

By examining this code and understanding how XOR multiplication works, you can see that it's actually quite straightforward once you understand the basics. The key is recognizing how each iteration represents a step in the multiplication process and how we use bitwise operations to perform the calculation.

Conclusion

In conclusion, XOR multiplication is an important concept in digital arithmetic that involves performing multiplication using only bitwise operations. By understanding how this works and how it's implemented in code, you can see that it's actually quite straightforward once you understand the basics. The key is recognizing how each iteration represents a step in the multiplication process and how we use bitwise operations to perform the calculation.

By using XOR multiplication, you can efficiently multiply numbers using only bitwise operations, which can be an important concept in digital arithmetic. By understanding how this works and how it's implemented in code, you can see that it's actually quite straightforward once you understand the basics.

"WEBVTTKind: captionsLanguage: enHey Frico Camp and welcome to another the daily program or webseries video and in this oneWe're going to talk about problem number 315 XORMultiplication which is posted on the daily program or subreddit and then before we dive into this problem remember to brush up on your bitwise?Operations because we're going to be using them in a solution come time to implement in Javascript, all rightSo let's break it down the whiteboardSo for this xor multiplication example. They give you two inputs rightThey give you an input A in this case is the integer 14 and then they give you input B?Which is another integer in this caseIt's 13And then the base two equivalents of these two integers would be 1 1 1 0 for 14 and 1 1 0 1 for 13so given this information their whole xor multiplication is defined like sofor every bit anInput b. Or in this case the bottom number 13We need to write outinput A ifthe bit is 1 so in this case the first bit on the right is a 1So we're going to go ahead and write outinput A andThen we've taken care of that bit so we can cross it up and then for the next part of the multiplicationyou just go ahead and indent 1 so I'll go ahead an indent 1 here andThen if it's a 0 for this bit on the far right that you've moved in 1 onwe just go ahead and write a 0 here and then repeat the process where you indent one more time andIn this case we're at a 1 so we need to rewrite this 14 out so I can say 0 1 1 1 andAgain, if it is a very last bitWe need to end it a third time and write out 0 1 1 1 andNow at this point. You can just do an xor operationon all the onesbeforehand so in this case if you xor 1 1 1 0 1 0 you get 1 1 1 0so I can go ahead and write that out here andYou get x or this line with this line you're going to go ahead and get1 10 11 0 and I'll ask you the x for this line with this line you're going to go ahead and get onezero zero zeroOne one zeroWhich will actually convert to the integer of seventy?So just to make sure that was kind of odd because I still can walk through it one last timeso I'll go ahead erase this andI'll rewrite this bottom string so remember the algorithm that they're presenting to you isThat for every bit in 13 starting from the right you need to write out the top oneIf it's a 1 and then indent for every bit, so let's do thisOne by one so again we'll cross that the one will write out and put a which is 14so then we get to the next bit zero andthen we have to indent one so let's go ahead and set a to zero here andThen we can xor these which leaves us with one one one zeroWe can cross out this bitWhich leaves us two indentations?And because the first one this is the second one and then write out input eggsx where those together andwe get one one zero one one zero andIt's finally we cross that this bitch. We end in threeright out input aandthen xor them all togetherWhich again leaves us with one zero zero zero one one zero which equates to 70?So again for this problemthey basically broke down the algorithm that we need to implement andThe only thing we need to do is just let galva Script and implement it. So let's go ahead and do that now, okay?So to start off let's go ahead and make a function called x or multiplyWe're going to take two parameters and be like we discussed on the whiteboard and down herelet's just go ahead and call it with 14 and 13 the same values that we did on the board andThen we can just go ahead and return null hereso the first thing I'm gonna do is I'm going to declare a results variable where we can keep track ofThe results when we keep x soaring a to a shifted to a shifted ETC, and then I could just say return results down hereso that function should make sense andThe first thing that we mentioned in the white board is that we want to keep looping over bUntil We run out of bits or run out of those you know digits 1 to 0?so we can say While b is not equal to 0We can just go ahead and keep shifting off bitsSo if you know anything about bitwise operations when you do this operation, we're pretty much saying take the integer BTake its base to representation and shift a bit off to the rightso if it was 1 1 0 1 it will now become 1 1 0 and then 1 1 and then 1 and we canactually show this if I also go ahead and say console.log beat out 2 string Base 2And C console is not defined, so it prints outAgain the first iteration the first thing and loop is going to print out 1 1 0 1 andThen it prints out 1 1 0 and then it prints out 1 1 1 and then 1So it's going to be four times and then one time for every character right until we reach 0so hopefully this part makes sense and then again toDemonstrate the other part you know step two of that algorithm isevery time you do an iteration you need to shift a over to the left one so you need to add a zero to aAnd what we can do is the opposite we can say a less than less than equal one whichIs again a bitwise operation which will push the integer a of the base to representation over oneso again I can say console.log a two strings a base two andWe see each iteration. It adds a zero to the endso now for the last partremember all we do all we have to do is just take a result and keep x oaring it with a if the bit ofB on the far right is equal to one so I can say result x or equals aWhich won't necessarily give us which our site results?So that won't give us the answer that we're looking for because we need to only x orwhen the rightmost right bit of b is equal to 1 so we can times it byB and then check if the right bit is on and we get our final answer of 70 down here. IWas going to print out this two stringSo again we can kind of walk through this and get a better understanding what's going on. So the first iterationB is 1 1 0 1 and a is 1 1 1 0 andthen we need 2 times 1 1 1 0By the first bit and B, and it's 1 in this case the result it becomes 1 1 1 0 and then the wayWe're going to shift over a shift over bthe loop goes on One more where b is equal to 1 1 0 anow has an extra 0 at the end andAgain, we check if the most far-right bit is on for B. And if it is we just xor results with a againWhich leaves us with the exact same results down here?So you see that results for the first iteration is 1 1 1 0 second iteration is also 1 1 1 0 andthen for the third iterationB is equal to 1 1 the most right bits on so therefore we xor results with 1 1 1 0 which is aAnd we're left with this and then we again repeat it for the last oneAgain be sure to like the video. We thought it was good and be sure to subscribe to Frito Camp below alright. Thanks for watching\n"