What does this line of code mean?

I’ve been downloading code snippets and playing around with stuff.

I found a function that converts decimal numbers into a string that represents the binary value. The problem is that I have drawn a blank regarding one statement in the function:



            DecToBin = CStr((DeciValue And 2 ^ i) / 2 ^ i) & DecToBin



The problem is (DeciValue And 2^i). As far as I can tell, it evaluates two numbers and then divides the result by 2^1 after that.

After plugging in some static values for testing, I still don’t get it.

Using just (DeciValue And i) in a For loop and outputting the value to the console:

DeciValue = 10
i=0-7
result: 0,0,2,2,0,0,2,2

DeciValue = 20
i=0-7
result: 0,0,0,0,4,4,4,4

DeciValue = 30
i=0-7
result: 0,0,2,2,4,4,6,6

DeciValue = 40
i=0-7
result: 0,0,0,0,0,0,0,0

DeciValue = 50
i=0-7
result: 0,0,2,2,0,0,2,2
I am most likely just having a stupid day and this is the most obvious thing in the world but I can’t seem to make heads or tails of it.
Thanks for any help.

Might be a piece of dummy code that can be used to verify if you are stealing someone esle’s code.

Referenced here
A small function to convert decimal integer values to a binary string. The number of bits can be optionally specified but this will be increased if insufficient.

2^i generates a number with just the i’th bit set:
i = 0 to 7:
0, 1, 10, 100, 1000, 10000, 100000, 1000000 (all binary).

“Anding” that with your decimal value gives you 2^i if the ith bit is set in the decimal value ( 1 AND 1 = 1) or zero if it’s not (0 AND 1 = 0). So the division gives you either 1 or zero. So by the end of the division, you’ve got a 1 if the i’th bit is set, and a zero if it’s not.

The CStr() converts that 1 or 0 to a string, and the & appends it to the front of the existing value. Do this for all looping values, and you’ve converted to binary.

(Decivalue AND i) won’t give you anything interesting, it’s just reporting those bits that happen to be in common between the two.

More interesting would be to do (Decivalue AND 2^1) if you want to see this work:
result: 0, 2, 0, 8, 0, 0, 0, 0

Which, you’ll notice, adds up to 10. If you convert the nonzeros to 1 (which is what the division does)
result: 0, 1, 0, 1, 0, 0, 0, 0

And reverse the order, which is what the append does:
result 0, 0, 0, 0, 1, 0, 1, 0

And interestingly enough, the binary value of 10 is: 1010.

Sufficient, or do I need to do another example?


DecToBin = CStr((DeciValue And 2 ^ i) / 2 ^ i) & DecToBin

(DeciValue And 2^i) / 2^i evaluates to 1 if there’s a 1 in the i-th bit of binary representation DeciValue and 0 otherwise. CStr() converts it to a string and “&” is the concatination operator.

So assuming DeciValue is 9 (binary: 1001) here’s how that line would execute (i’m assuming it’s iterated over i = 0 through i = 8, 16 or 32)

DecToBin == “”

First run: (DeciValue And 2^0) == 1, (DeciValue And 2^0) / 2^0 == 1
so DecToBin = “1” & “”;

DecToBin == “1”

Third run: (DeciValue And 2^2) == 0, (DeciValue And 2^2) / 2^2 == 0
so DecToBin = “0” & “01”;

DecToBin == "001"Second run: (DeciValue And 2^1) == 0, (DeciValue And 2^1) / 2^1 == 0
so DecToBin = “0” & “1”;

DecToBin == “01”

Fourth run: (DeciValue And 2^3) == 8, (DeciValue And 2^3) / 2^3 == 1
so DecToBin = “1” & “001”;

DecToBin == “1001”

and so on…

Hope this makes it clear…

  • Groman

P.S. I don’t know VB so I might’ve goofed it up. I’m assuming “And” is bitwise in this case and “&” string concat.

And, of course, I screwed up the list at the beginning: 2^0 = 1, not zero, so take off that first zero. But I think the rest is right.

No. I get it. Like I said, stupid day but it was really bugging me. Thanks TimeWinder. Maybe I should just work on code tomorrow :slight_smile:

Thanks astro for the link. That was where I got it from but lost the link.

Your assumption is correct groman. Thanks as well.

Aww, too late, I was actually coding because they don’t like me surfing the dope too much at work. :smiley:

I call it research… or something - so it’s all good :stuck_out_tongue: