-3

I am currently working on a script which find and print the base-10 integer denoting the maximum number of consecutive 's in 's binary representation.

I would like someone to explain for me a line, which is as follows:

n = (n & (n<<1));

I know that n<<1 moves the binary number of n to left, but about the rest of the code?

Hodormad
  • 21
  • 1
  • 5
  • `n<<1` returns n multiplied by 2, then a binary AND is performed between that result and n. – Déjà vu Aug 09 '21 at 00:55
  • @Hodormad 'This community is done so we can learn from each other' not exactly, no. This is not a site that provides one-on-one tuition on basic principles, eg. bitwise logic and boolean algebra in C. A textbook or tutorial site is a better resource for learning such things. – Martin James Aug 09 '21 at 03:47
  • @Breakingnotsobad 'the guy is a beginner and the syntax is pretty difficult' well, then this is an inappropriate place to get basic tuition. Also, the line is a simple expression and assignment statement, it is not 'difficult' for a professional/enthusiastic programmer and C bitwise operations are easy to look up. Every beginner textbook or site has that info. – Martin James Aug 09 '21 at 03:54
  • It's one thing to know that `n & (n<<1)` shifts `n` left by one bit and then does a bitwise AND with itself. It's quite another thing to know *why* someone would do that, or *what* useful result (if any) is obtained. *That's* certainly not a basic question! (I, for one, do not know the answer, although @thebesttv's answer seems to have some clues.) – Steve Summit Aug 09 '21 at 05:56
  • Note to the reader: my name is mentioned here without my comments that were deleted (not by me!) – Déjà vu Aug 09 '21 at 08:18
  • @Breakingnotsobad My comments were deleted too – Hodormad Aug 10 '21 at 00:04

2 Answers2

1

So Lets break it down,

Firstly (n << 1) is same as (n * 2).

Here is a live example.(Its in JavaScript but these operators are same everywhere)

function calc() {
  var n = parseInt(document.getElementById("n").value);
  var result = n << 1;
  var originalBin = toBinary(n);
  var resultBin = toBinary(result);
  document.getElementById("r").innerText = `
  Original Number: ${n}
  Binary(Original): ${originalBin}
  Binary(Shifted): ${resultBin}
  `;
}

function toBinary(n) {
  let binary = "";
  if (n < 0) {
    n = n >>> 0;
  }
  while (Math.ceil(n / 2) > 0) {
    binary = n % 2 + binary;
    n = Math.floor(n / 2);
  }
  return binary;
}
<input id="n" type="number">
<br>
<button onclick="calc()">Calculate</button>
<br>
<b id="r"></b>

Now for the second part the & operator.

Quoting from https://learn.microsoft.com/en-us/cpp/cpp/bitwise-and-operator-amp?view=msvc-160

The bitwise AND operator (&) compares each bit of the first operand to the corresponding bit of the second operand. If both bits are 1, the corresponding result bit is set to 1. Otherwise, the corresponding result bit is set to 0.

Both operands to the bitwise AND operator must have integral types. The usual arithmetic conversions covered in Standard Conversions are applied to the operands.

Jaysmito Mukherjee
  • 1,467
  • 2
  • 10
  • 29
1

I guess you're trying to say "the maximum number of consecutive 1's in n's binary representation? Try an example (if n is one-byte long):

       n =   1110 1011
  n << 1 = 1 1101 0110
n&(n<<1) =   1100 0010

The binary representation of n has the form 111 (2 consecutive 1's), 11 (1 consecutive 1) and 1 (0 consecutive 1). n << 1 will shift the leftmost 1 to the ninth bit, which will be discarded. The result has 3 1's, which is the answer.

Then if you need a count (in the example, 3), you just count the number of 1's in n.

thebesttv
  • 36
  • 4