# Setting the equivelent binary bit

So I have an integer and I want to set the equivelent binary bit NOT convert it to binary. Ie
integer = 4
therefore
binary value = 0b00001000
I think I need bitwise OR and a for loop checking if a mask is at the right “bit”, but i cant think quite how to do it. I want to then pass that to this function.

``````void leds(unsigned int integer){
for(int i=0; i<12; i++){
if((mask & integer) == 0) digitalWrite(pins[i], LOW);
else digitalWrite(pins[i], HIGH);
}
}
``````

which goes through each bit and lights the led if that bit is a 1 (I have 12 leds in a row you see…)
i think I want OR because I want to be able to call the function twice, change the state of another bit and then light both those leds…

Like so?

``````int mask;

void setup(void)
{
Serial.begin(115200);
for (int i = 1; i <= 8; i++) {
mask = 1 << (i - 1);
Serial.print(i, DEC);
Serial.print(' ');
}
}

void loop(void)
{
}
``````

Output:

``````1 1
2 10
3 100
4 1000
5 10000
6 100000
7 1000000
8 10000000
``````

Thanks, i can work with that…
EDIT:
so can I do:

``````int mask;
int result;
for (int i = 1; i <= 12; i++) {
mask = 1 << (i - 1);
if (i == Mappedkey()){
}
}
return result;
}
``````

Will that overwrite result if I call it twice, I.e using thi method can I call that function twice and set to bits to 1?

You can do that, but I’m not sure the loop is necessary, I only put that in as a demo. It can be a function, but it’s so simple I’m not sure I’d bother making it a function (and I have been known to code some pretty simple functions). I think all that is needed is

``````mask = 1 << (i - 1);
``````

So I am getting there with this, but still struggling slightly:
I have 12 buttons, and therefore 12 binary bits. Each button is numbered one to twelve, and I want to set the equivalent binary bit to 1 when pressed, and clear it when released. However if two are pressed I want both set to one, and if one is released I just want to clear that bit, leaving the other one.
When a button is pressed, I am using:

``````binaryMask |= 1 << (button);
``````

to set it.
When a button is released I am using

``````binaryMask &= ~(1 << (button));
``````

however clearing it is having spuradic results. Is this clearing everything or something??
Should I be using something like an OR to clear the bit?

P.S what is the final binaryMask looking like if button = 1 - 12 in this case. I.e which one is 1 and which one is 12:
button = 1
button = 12 = 0b100000000000
??

Just use _BV(),

_BV(3) would be 100, _BV(7)==1000000

So say you were working with a port and wanted to turn a pin on it would be PORTD |= _BV(bit);

It’d help to be able to see all the code. Seems like this is just a matter of reading 12 buttons and building up the number. Not sure what the trigger is to do this, or what happens with the resulting number. Which button represents the least significant bit and which the most significant depends on how things are defined and physically laid out. Hint, normally the rightmost bit (the least significant bit) would be called “bit 0” and the most significant bit would be “bit 11”. Makes it a little easier to think about because bit 0 represents 20, bit 1 represents 21, bit 2 represents 22 … bit 11 represents 211.

Cool, that _BV trick did it nicely, thanks.
My code is really quite big, and to make a smaller version to show you would be quite tricky…
Basically I have it now storing button presses in a binary number, then I use a loop to go along the binary number looking for 1’s and if it is a 1, turn on the corresponding led.
In light of that, if i wanted to go through the 1s randomly, what would you recommend - obviously I could randomly go through the entire binary number, but if only one bit is set to 1 out of 12, in an unlikely situation I could be waiting ages to see an led - timing between flashes is critical I was thinking of also storing the button presses in an array and radomly picking elements, however I was struggling with removing elements, if one of a few buttons was released:

``````//Adding/removing from array for ordered arpeggiator functions.

pressedNoteOrder[sizeOfArray+1] = MappedKey-1;
sizeOfArray++;
}
void removeFromArray(int MappedKey){

for (byte i = 0; i<sizeof(pressedNoteOrder); i++){//go through each item in the array
if(pressedNoteOrder[i] == MappedKey-1){//look for a match to the note that was released
for (byte j=i+1; j<sizeof(pressedNoteOrder); j++){
pressedNoteOrder[j-1] = pressedNoteOrder[j];//when found, shift each note after down one, i.e to clear it out of the array
}
break;
}
}
pressedNoteOrder[sizeof(pressedNoteOrder)-1] = 0;
sizeOfArray--;
}
void playArray(){
static int i = -1;
i++;
if (i > sizeOfArray){
i = 0;
}
Play(pressedNoteOrder[i]);
}
``````

tips appreciated ;D

Not getting the random thing, what would the point or advantage of that be. Pretty tough to provide good answers without understanding the overall concept of the project.

Lol I hope you know what that code does I had a hard time following it

Its just trying to find an element in the array that matches the value of a variable and shift all elements after that one back one, i.e deleting the element from the array. Hard to write - Im not sure its doing what I want anyway.

In terms of the random thing, if you had the biinary value
101010
then leds (in order) light would be 6,4,2
but if I want those ones to light randomly, i.e 6,2,6,4,2,4,4,2,2,6,2,6,2,6
etc then what would be the best way of doing this? because obviously I cant just pick randoms from the original binary number because i could end up picking leds that wont light alot of the time - especially if there are many more 0's than 1's in the binary number...