# Decimal to binary boolean array

I’ve hit a roadblock. Ive made a game which is played on a 8*8 led matrix that also tracks player score. I’d like to be able to display the score at the end of the game, as lit leds, so we’ve got 8 bits to work with.

The score is stored as an int called score... and that’s as far as I’ve gotten.

I’m confident that I can figure out how to display the score if I have it as binary in a Boolean array. But I simply have no idea how to do the conversion.

I’ve seen quite a number of topics on different ways of doing the decimal to binary conversion, but not one I could use.

To summarize: an int smaller than 256 needs to be converted to binary and stored in a Boolean array.

Any help is greatly appreciated!!

MathiasAWK: I’ve hit a roadblock. Ive made a game which is played on a 8*8 led matrix that also tracks player score. I’d like to be able to display the score at the end of the game, as lit leds, so we’ve got 8 bits to work with.

The score is stored as an int called score... and that’s as far as I’ve gotten.

I’m confident that I can figure out how to display the score if I have it as binary in a Boolean array. But I simply have no idea how to do the conversion.

I’ve seen quite a number of topics on different ways of doing the decimal to binary conversion, but not one I could use.

To summarize: an int smaller than 256 needs to be converted to binary and stored in a Boolean array.

Any help is greatly appreciated!!

Question:

How are you driving the LED?

Are they connected directly to your arduino or u using a shift register?

Depending on your answers the approach would be different (neither involves the conversion you think btw! )

The processor doesn’t know decimal, only binary.
There’s no need to convert an integer to binary, because it already is binary.
Converting a binary integer to an array of boolean values is a waste of resources.

If you want to know if a bit is set, you mask the position of that bit. The mask for bit n is (1 << n), where n = 0 is the rightmost bit. Applying the mask is just a bitwise AND operation.

Let’s say you have a 4-bit binary number 0b0101.
0b0101 & (1 << 0) = 0b0101 & 0b0001 = 0b0001 ≠ 0 → bit 0 = 1
0b0101 & (1 << 1) = 0b0101 & 0b0010 = 0b0000 = 0 → bit 1 = 0
0b0101 & (1 << 2) = 0b0101 & 0b0100 = 0b0100 ≠ 0 → bit 2 = 1
0b0101 & (1 << 3) = 0b0101 & 0b1000 = 0b0000 = 0 → bit 3 = 0

Pieter

First off, I’m sorry I didn’t answer right away, I’ve been tinkering with this problem for a while, so I fell asleep shortly after posting.

The led matrix is its own thing, all controlled directly through a single pin.

To Pieter: I guess I know that the data I’m getting is technically binary, but I don’t understand your answer, do you mean to say that I should be storing the data as binary to begin with? I’m kind of a novice programmer, and it’s a lot easier for me to work with ints for doing any calculation or comparison, and then converting the number when I need to display the information.

MathiasAWK: The led matrix is its own thing, all controlled directly through a single pin.

Ok that sounds like a shift register.

Do you have the a datasheet for it?

what you could you if you know what 'bit' turn what LED is create a look up array.

so for example you have you score as 9, matix_arr[9] corresponds to 0b10101010 (DEC 170) to make the matrix display figure '9'

shift out (that method to be yet detemined) matix_arr[score] to the LED matrix.

Sadly, the matrix was out of whatever box it came in, and neither me nor my teachers have found its corresponding datasheet.

Is as close as we’ve come. I’m sure my idea of how to display the score is very inefficient, but I’m sure I’d be able to do it. I’d run a for loop through the bool array and for every true position I’d light an led.

MathiasAWK: Sadly, the matrix was out of whatever box it came in, and neither me nor my teachers have found its corresponding datasheet.

Is as close as we’ve come. I’m sure my idea of how to display the score is very inefficient, but I’m sure I’d be able to do it. I’d run a for loop through the bool array and for every true position I’d light an led.

If that is indeed the LED you are using there's LOADs of material out there on how to control them. just one of many...