Binary Number Generation

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Murdz
    New Member
    • Aug 2007
    • 34

    #1

    Binary Number Generation

    Hello.

    I want to be able to generate large binary numbers in C#, based on an integer, and later perform a bitwise operation on them - I'm talking being able to support numbers up to (and hopefully even beyond, just in case, 2^100)

    The basis is that I have a database table with roughly 70 rows, each row has a unique identity value (1, 2, 3, 4, etc) and from that ID I want to generate a matching binary number, something like 2^(ID - 1). So, the binary ID for the row ID of 3 would be 4 = 2^(3-1). The binary ID for a row ID of 4 would be 8 = 2^(4-1). etc, etc. Any ideas on how to go about this? Or even what datatypes to use?

    For those who care about the reasoning; I have a webpage in which users can view one, more or all of the values within this table and rather than pass 70+ IDs in the querystring I wanted to be able to pass in a binary number representing the selected rows; eg, Rows=9 would mean showing rows 1 and 4 (2^(1-1) = 1 + 2^(4-1) = 8) = 9.

    Thanks
  • GaryTexmo
    Recognized Expert Top Contributor
    • Jul 2009
    • 1501

    #2
    I did a google search for large numbers in C# and found a few hits. It looks like you're pretty much going to have to make your own data type, which means if you want bit-wise operators on them, you'll have to implement them yourself.



    Another approach you might consider is using several long ints.

    For example... lets say you have an array of 10 unsigned integers, each one representing 32 rows in your database.

    flags[0] = rows 0 to 31
    flags[1] = rows 32 to 63
    flags[2] = rows 64 to 95
    ...
    flags[n] = rows (n * 32) to (n * 32 + 31)

    You can't do a single bitwise operator on it, but you can parse through and figure out which rows are "set to return" fairly easily. It might save you some trouble in designing and implementing a big integer class.

    Good luck!

    Comment

    • Murdz
      New Member
      • Aug 2007
      • 34

      #3
      Yes I actually found that BigInteger datatype an hour or two after posting this and it does seem to solve the problem but thank you for your comment.

      Comment

      Working...