String to Bitmask

12 09 2014

Going to be a short post and unlike other times when I’ve said it and then went on to having a series of four posts, this time I mean it.

So, first, what brought this on?

Well, having tired somewhat of Unity‘s implementation I’ve decided to play around and write my own Entity.Component.System framework. For a game (of which I shall write about at a later date when there’s something playable).

In the process of writing it I’ve come to the conclusion that I need to twiddle some bits and because I’m quite pedantic it means I need to write tests for this.

Given my unit testing framework (NUnit) I’ve run into some issues when using the “TestCase” attribute. This (and other considerations) resulted in me digging through some old utilities and presto! Converting a string representation to a bitmask. A quick cleanup (it was written in C) and, presto! Let me introduce you all to the new utility on the block.

public static class GenerateBitmaskFromString
 public static Bitmask Perform(string line)
  var mask = 0L;
  for (var i = line.Length - 1; i >= 0; --i)
   if (line[i] == '1')
    mask |= 1L << ((line.Length - 1) - i);
  return new Bitmask(mask);

The Bitmask object itself is a simple struct wrapper around an Int64 or Int32 (depending on the platform) which provides some extra twiddling (is that… is that a word?) functionality.

I’m assuming knowledge of bit shifting and binary logic by people reading this. And also note that the order of character processing is reversed since given strings are usually in the form of “0000001” with their first character being the left most (ie. length-1) while bits are shifted rightwise.

Well, that’s it. Not very epic or grand but useful nonetheless.

He said, “We haven’t had that spirit here since nineteen sixty nine”


Take 10 of these

14 09 2010

And call me in the morning…

I’ve been sick, well, actually I’m still sick but a couple of pills can go a long way to dull out a headache. It’s still hard to think so I’ll keep this post short and just talk a bit about binary.

I remember my first class of computer science, the teacher was/is an… interesting… character [but in a good way]. He walked into the classroom, yelled out his name and asked us how do we order 31 oranges if we only have one hand…

Binary is not a difficult base, not any more than any other base, my biggest hurdle of it at first was my inability to separate the value of a number from its representation. Let’s make some orange juice.

Say I have five oranges; I can count them, physically in front of me are five oranges. I can use a decimal representation for them “5” or I can invent my own representation of that value “*”. Either way, the value doesn’t change, it is the symbol representing them which does.

Right, now we get to the base representation. If we already know how to use decimal numbers [base 10] then really we know how to use all bases.
Base 10 [Decimal]
Value range: 0->9
1000 100 10 1
  0   0  3  1

Our representation here reads: 31
Let’s calculate the actual value though;
So our value is the same as our representation [makes sense since I used decimal representation to represent the value in the first place].

now in binary.
Base 2 [Binary]
Value range: 0->1
16 8 4 2 1
 1 1 1 1 1

Our representation reads: 11111
Let’s calculate the actual value;

Same value, different representation. Notice the similarities between the base representations:
Base X
Value Range: 0->(X-1)
...(X*X*X) (X*X) (X) 1

With computers we find that two bases are important, base 2 [Binary] and base 16 [Hexadecimal] because:
A. Computers “think” in binary, everything we write is actually encoded electronically as [1010101].
B. Hexadecimal representations are more compact than binary and are easily converted to binary [every bit field in the hexadecimal column corresponds to 4 bits in binary].

That’s it, nothing too complicated but it taught me an important lesson; don’t confuse the value of an object with the representation of its value. My pills are running out and my head is throbbing again but at least the dishes are done.

“Who’s gonna cry when old John dies?”