Find the binary equivalent of a number

I have set up a function that acts like the first 8 digits of binary. Im trying assign each value in the binary code with its decimal equivalent. Look below too see what im trying to do.

verlistset[0,0,0,0,0,0,0,0,0]
verlistset[1,0,0,0,0,0,0,0,1]
verlistset[0,1,0,0,0,0,0,0,2]
verlistset[1,1,0,0,0,0,0,0,3]
verlistset[0,0,1,0,0,0,0,0,4]
verlistset[1,0,1,0,0,0,0,0,5]
//...247 lines later...
verlistset[0,1,1,1,1,1,1,1,254]
verlistset[1,1,1,1,1,1,1,1,255]

This is very inconvenient, because it would require adding 256 lines of code, which would take forever to type and take up a lot of space. What I’m asking is for help to do this faster. My thoughts so far is to set up a loop like this:

for(i=0;i<256;i+=1){
x1= //somthing
x2= //somthing
x3= //somthing
x4= //somthing
x5= //somthing
x6= //somthing
x7= //somthing
x8= //somthing
verlistset[x1,x2,x3,x4,x5,x6,x7,x8,i]
}

But I have no Idea how to find the “somthing” value in the code above. Does anyone know how to help?

You can find a C# example to convert to a 32-bit binary string here, which should be easy to convert to your array.

In JavaScript you can do something similar with toString(2).

More on logical shift and bitwise operations at Wikipedia.

Uhm, you know that there are binary operators? Like:

  • or: |
  • and: &
  • xor: ^

What’s the actual purpose of your strange array? How would you use it once it’s initialized? Also if this isn’t just pure pseudo code, what do you actually store in this nine-dimensional array?

Anyways. If you have an arbitrary number, for example “42”. If you want to know the binary representation you just have to test for each bit:

((42 &    1) != 0) = false
((42 &    2) != 0) = true
((42 &    4) != 0) = false
((42 &    8) != 0) = true
((42 &   16) != 0) = false
((42 &   32) != 0) = true
((42 &   64) != 0) = false
((42 &  128) != 0) = false

This is the same with a left shift operator:

(42 & (1<<0)) != 0
(42 & (1<<1)) != 0
(42 & (1<<2)) != 0
(42 & (1<<3)) != 0
(42 & (1<<4)) != 0
(42 & (1<<5)) != 0
(42 & (1<<6)) != 0
(42 & (1<<7)) != 0

This simple function converts an arbitrary integer into a binary string:

// C#

string ToBinary(int aVal, int aDigits)
{
    string result = "";
    for(int i = 0; i < aDigits; i++)
    {
        if (aVal & (1 << i) != 0)
            result = "1" + result;
        else
            result = "0" + result;
    }
    return result;
}

Examples:

Debug.Log("56 == " + ToBinary(56,8));       // "56 == 00111000"
Debug.Log("56 == " + ToBinary(56,16));      // "56 == 0000000000111000"

If you are using that array from your other question you can do:

  for(var i = 0; i < 256; i++) eightDimensionalArray *= i;*

I am beginning to thing that you should be using bits though, you could do that easily for marching cubes like this:
const upperTopLeftCorner = 1 << 0;
const upperTopRightCorner = 1 << 1;
const upperBottomLeftCorner = 1 << 2;
const upperBottomRightCorner = 1 << 3;
const lowerTopLeftCorner = 1 << 4;
const lowerTopRightCorner = 1 << 5;
const lowerBottomLeftCorner = 1 << 6;
const lowerBottomRightCorner = 1 << 7;
Then to work out which of your cubes to use.
* Set an int value to 0
* Test each point to see if it is occupied
* If it is then or the value with the current corner
* Your final value is you lookup into your predefined cubes!
var template = 0;
if(UpperTopLeftCornerIsOccupied()) template |= upperTopLeftCorner;
if(UpperTopRightCornerIsOccupied()) template |= upperTopRightCorner;

At the end of that you will have the number between 0 and 255 which is the template shape you should use.

Thank you GerryM! The link you lead me to required a little changing, but I got it to work. Here is what I did:

function GetIntBinaryString(n:int)
    {
	var b : int[] = new int[8];
	var pos : int = 7;
	var i : int = 0;
	while (i < 8)
	{
	    if ((n & (1 << i)) != 0)
	    {
		b[pos] = 1;
	    }
	    else
	    {
		b[pos] = 0;
	    }
	    pos-=1;
	    i+=1;
	}
	verlistset(b[7],b[6],b[5],b[4],b[3],b[2],b[1],b[0],n);
    }

for (var n=0;n<256;n+=1){
GetIntBinaryString(n);
}

This code sets my all of the 8 digits of binary to its decimal equivelant. Now I don’t need 256 lines of code. Thanks again!