• ### Binary String Incrementation

Yeah. I was kind of bored. I wrote this up. You could use a similar method to retrieve a Oct or Hex string too. I might re-write it to let it support different numeral systems if I have time.

``````static void Main(string[] args)
{
string binary = AbsoluteZero();
int count = 0;

while (count < 256)
{
count++;
Console.WriteLine(count + ":  " + binary);
binary = IncrementBinary(binary);
}

}

static bool isBinaryString(string binary)
{
// Is the binary value over 8 characters?
if (binary.Length > 8)
return false;

// Does it contain illegal characters?
foreach (char bit in binary)
if (!bit.Equals('0') && !bit.Equals('1'))
return false;

// Everything passed.
return true;
}

static string AbsoluteZero()
{
return "00000000";
}

static char IncrementBit(char bit)
{
switch (bit)
{
case '0':
return '1';
case '1':
return '2';
default:
return '0';
}
}

static string IncrementBinary(string binary)
{
if (isBinaryString(binary))
{
// Get all the bits, and increment the first one.
char[] bit = binary.ToCharArray();
bit[7] = IncrementBit(bit[7]);

// Loop through all the bits, and find out
// if we can push over.
for (int i = bit.Length - 1; i >= 0; i--)
if (bit[i].Equals('2'))
{
// Make sure the index isn't 0\. Otherwise, we'd get an RTE trying to
// access the bit of the -1 index.
if (i != 0)
{
// Increment the next bit, and the current bit.
bit[i] = IncrementBit(bit[i]);
bit[i] = IncrementBit(bit[i]);
}
else
{
// This chunk of code will only be run if the current
// bit index is 0, and we need to push it over. So,
// just change it to 0.
bit[0] = '0';
}
}

// Return the binary string.
return new string(bit);
}
else
{
// Our binary string wasn't a valid one. Return the lowest possible binary
// value.
return AbsoluteZero();
}
}

[/i][/i][/i][/i][/i]
``````
• X

### Binary String Incrementation

``````
static void Main(string[] args) {
int number = Convert.ToInt16("0101", 2);
++number;
Console.Out.WriteLine(number);
Console.Out.Write(Convert.ToString(number, 2));
}
``````

ik its sad but true

1 weird thing actually worth discussing is that my method is about twice as fast when running on my pc :

while urs is actually also about twice as fast on 1 online compiler :

heres the performance test source if the ideone link goes down :

http://pastebin.com/raw.php?i=9z7yL6Q5

• M

### Binary String Incrementation

Depending on your compiler it will do a ton of optimizations for you automatically. In this case of a hardcoded value it may just return the value without doing any of your code at all. Where because his the argument changes it has to do a bit more work.

• X

### Binary String Incrementation

Where because his the argument changes it has to do a bit more work.

i what cuz feel argument 3 same

In this case of a hardcoded value it may just return the value without doing any of your code at all.

wat hardcoded value?

Depending on your compiler it will do a ton of optimizations for you automatically.

yes i agree, thats the most probable source of the problem. but theres not that much c# compilers going around. i tested on vs, they most likely run mono. y such disrepency between the two methods?

• M

### Binary String Incrementation

I honestly have no idea what your first question is.

Your whole function is hard coded. You typed numbers in.

• X

### Binary String Incrementation

Your whole function is hard coded. You typed numbers in.

its 1 retarded mistake, fixed it, same efficiency results.

Posts 6Views 1669