Skip to main content
typos
Source Link
JanDotNet
  • 8.5k
  • 2
  • 20
  • 48

What about:

var value = 529;
var count = 0;
var countMax = 0;
while(value > 0)
{
    count = value % 2 == 0 ? count + 1 : 0;
    value /= 2;
    countMax = Math.Max(count, countMax);
}
// result in: countMax;

[Edit: Version that supports also negative ints]

var value = 529;
var count = 0;
var countMax = 0;
var factor = value < 0 ? -1 : 1;
while((value * factor) > 0)
{
    count = value % 2 == 0 ? count + 1 : 0;
    value /= 2;
    countMax = Math.Max(count, countMax);
}
// result in: countMax;

[Edit2: added explanation]

Converting the integer to a string of bits and count the linked zeros using string operations is one option. However, it needs lots of code and is not realy performandreally performant.

Another option is to check if the number is even. If so, the last bit is not set ('0'). Dividing the number with 2 removes the last bit, so that we can check the next bit... and so on.

What about:

var value = 529;
var count = 0;
var countMax = 0;
while(value > 0)
{
    count = value % 2 == 0 ? count + 1 : 0;
    value /= 2;
    countMax = Math.Max(count, countMax);
}
// result in: countMax;

[Edit: Version that supports also negative ints]

var value = 529;
var count = 0;
var countMax = 0;
var factor = value < 0 ? -1 : 1;
while((value * factor) > 0)
{
    count = value % 2 == 0 ? count + 1 : 0;
    value /= 2;
    countMax = Math.Max(count, countMax);
}
// result in: countMax;

[Edit2: added explanation]

Converting the integer to a string of bits and count the linked zeros using string operations is one option. However, it needs lots of code and is not realy performand.

Another option is to check if the number is even. If so, the last bit is not set ('0'). Dividing the number with 2 removes the last bit, so that we can check the next bit... and so on.

What about:

var value = 529;
var count = 0;
var countMax = 0;
while(value > 0)
{
    count = value % 2 == 0 ? count + 1 : 0;
    value /= 2;
    countMax = Math.Max(count, countMax);
}
// result in: countMax;

[Edit: Version that supports also negative ints]

var value = 529;
var count = 0;
var countMax = 0;
var factor = value < 0 ? -1 : 1;
while((value * factor) > 0)
{
    count = value % 2 == 0 ? count + 1 : 0;
    value /= 2;
    countMax = Math.Max(count, countMax);
}
// result in: countMax;

[Edit2: added explanation]

Converting the integer to a string of bits and count the linked zeros using string operations is one option. However, it needs lots of code and is not really performant.

Another option is to check if the number is even. If so, the last bit is not set ('0'). Dividing the number with 2 removes the last bit, so that we can check the next bit... and so on.

added 391 characters in body
Source Link
JanDotNet
  • 8.5k
  • 2
  • 20
  • 48

What about:

var value = 529;
var count = 0;
var countMax = 0;
while(value > 0)
{
    count = value % 2 == 0 ? count + 1 : 0;
    value /= 2;
    countMax = Math.Max(count, countMax);
}
// result in: countMax;

[Edit: Version that supports also negative ints]

var value = 529;
var count = 0;
var countMax = 0;
var factor = value < 0 ? -1 : 1;
while((value * factor) > 0)
{
    count = value % 2 == 0 ? count + 1 : 0;
    value /= 2;
    countMax = Math.Max(count, countMax);
}
// result in: countMax;

[Edit2: added explanation]

Converting the integer to a string of bits and count the linked zeros using string operations is one option. However, it needs lots of code and is not realy performand.

Another option is to check if the number is even. If so, the last bit is not set ('0'). Dividing the number with 2 removes the last bit, so that we can check the next bit... and so on.

What about:

var value = 529;
var count = 0;
var countMax = 0;
while(value > 0)
{
    count = value % 2 == 0 ? count + 1 : 0;
    value /= 2;
    countMax = Math.Max(count, countMax);
}
// result in: countMax;

[Edit: Version that supports also negative ints]

var value = 529;
var count = 0;
var countMax = 0;
var factor = value < 0 ? -1 : 1;
while((value * factor) > 0)
{
    count = value % 2 == 0 ? count + 1 : 0;
    value /= 2;
    countMax = Math.Max(count, countMax);
}
// result in: countMax;

What about:

var value = 529;
var count = 0;
var countMax = 0;
while(value > 0)
{
    count = value % 2 == 0 ? count + 1 : 0;
    value /= 2;
    countMax = Math.Max(count, countMax);
}
// result in: countMax;

[Edit: Version that supports also negative ints]

var value = 529;
var count = 0;
var countMax = 0;
var factor = value < 0 ? -1 : 1;
while((value * factor) > 0)
{
    count = value % 2 == 0 ? count + 1 : 0;
    value /= 2;
    countMax = Math.Max(count, countMax);
}
// result in: countMax;

[Edit2: added explanation]

Converting the integer to a string of bits and count the linked zeros using string operations is one option. However, it needs lots of code and is not realy performand.

Another option is to check if the number is even. If so, the last bit is not set ('0'). Dividing the number with 2 removes the last bit, so that we can check the next bit... and so on.

Added additional example that supports negative ints
Source Link
JanDotNet
  • 8.5k
  • 2
  • 20
  • 48

What about:

var value = 529;
var count = 0;
var countMax = 0;
while(value > 0)
{
    count = value % 2 == 0 ? count + 1 : 0;
    value /= 2;
    countMax = Math.Max(count, countMax);
}
// result in: countMax;

[Edit: Version that supports also negative ints]

var value = 529;
var count = 0;
var countMax = 0;
var factor = value < 0 ? -1 : 1;
while((value * factor) > 0)
{
    count = value % 2 == 0 ? count + 1 : 0;
    value /= 2;
    countMax = Math.Max(count, countMax);
}
// result in: countMax;

What about:

var value = 529;
var count = 0;
var countMax = 0;
while(value > 0)
{
    count = value % 2 == 0 ? count + 1 : 0;
    value /= 2;
    countMax = Math.Max(count, countMax);
}
// result in: countMax;

What about:

var value = 529;
var count = 0;
var countMax = 0;
while(value > 0)
{
    count = value % 2 == 0 ? count + 1 : 0;
    value /= 2;
    countMax = Math.Max(count, countMax);
}
// result in: countMax;

[Edit: Version that supports also negative ints]

var value = 529;
var count = 0;
var countMax = 0;
var factor = value < 0 ? -1 : 1;
while((value * factor) > 0)
{
    count = value % 2 == 0 ? count + 1 : 0;
    value /= 2;
    countMax = Math.Max(count, countMax);
}
// result in: countMax;
Source Link
JanDotNet
  • 8.5k
  • 2
  • 20
  • 48
Loading