Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

Binary String Incrementation


Matt
 Share

Recommended Posts

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);
}

Console.ReadKey();
}

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]
```
Link to comment
Share on other sites

```

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

```
 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 :

![](http://i.imgur.com/LzbpgA7.png)

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

![](http://i.imgur.com/K7CLzcE.png)

c [http://ideone.com/bTmhVJ](http://ideone.com/bTmhVJ)

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

[http://pastebin.com/raw.php?i=9z7yL6Q5](http://pastebin.com/raw.php?i=MfYu9fPz)
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

> 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?
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...