# How to modify a created byte?

I have created a `byte` for 16x2 LCD. It is basically a custom character which I will have to change later.

``````byte char{
B10000,
B01000,
B00100,
B00010,
B00001,
B11000,
B11100
};
``````

I want to edit the third number from fourth row. So, the fourth row will become `B00110` from `B00010`. Is this possible? Are there any alternatives for this?

## 2 Answers

Well first of all you are trying to name the variable char? which is already a keyword for the variable type of char. But you can access it by the index of the array. Also not sure if you intended to make the array 8 bytes in length, and then only use 7, but thats what you had done in your example

``````//define it all at once
// byte byteArray = {B10000,B01000,B00100,B00010,B00001,B11000,B11100};
//or by index
byte byteArray;
byteArray=B10000;
byteArray=B01000;
byteArray=B00100;
byteArray=B00010;
byteArray=B00001;
byteArray=B11000;
byteArray=B11100;

//if you need to change one in the code elsewhere
byteArray=B00110;
//or
byteArray=6;
}
``````
• In the second last line, why did you set the `byteArray` equal to `6`? Why not `3` because we want to change the third element? – Black Thunder Jun 19 '19 at 11:34
• B00110 = B110 = 1 (first 1) * 2^2 + 1 (second 1) * 2^1 + 0 (last 0) * 2^0 = 1 * 4 + 1 * 2 + 0 * 1 = 6 – Michel Keijzers Jun 19 '19 at 12:05
• @MichelKeijzers Well, i didn't get it. What is the purpose of it? Is there simpler way? I just don't want to involve other elements. – Black Thunder Jun 19 '19 at 12:10
• E.g. in the normal (10 or decimal system) number 76 = 7 * 10^1 + 6 * 10^0 = 7 * 10 + 6 * 1 = 70 + 6 = 76. In the binary (2 system) number 110 = 1 * 2^2 + 1 * 2^1 + 0 * 2^0 = 1 * 4 + 1 * 2 + 1 * 0 = 4 + 2 + 0 = 6. – Michel Keijzers Jun 19 '19 at 12:30
• Was just showing that it could be set using decimal rather then binary x=B00110(Binary), x=6(Dec),x=0x06(Hex) Are all equal. It doesn't really pertain to your question, so I should have just left it out to not be confusing. – Chad G Jun 19 '19 at 16:08

You can use the following array initialization:

In the setup I shows how to set a bit. For this, the bit operator or (|) is used. To reset a bit, you can use &. You can set/reset multiple bits this way.

Also you can use 1 << 2 which means 1 (most right bit) shifted left two places (thus B100).

Btw, it is common practice to initialize all values.

``````byte lcd =
{
B10000,
B01000,
B00100,
B00010,
B00001,
B11000,
B11100,
B00000  // Also initialize last element
};

void setup()
{
lcd |= B100; // Set 4th row (element 3), 3th bit (from the right)
lcd |= 1 << 2; // Alternative
}

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

Explanation about the `or` arithmetics: OR means: if at least one bit is 1, the result is 1, otherwise 0.

Truth table:

``````A | B | A or B
--+---+-------
0 | 0 |   0
0 | 1 |   1
1 | 0 |   1
1 | 1 |   1
``````

For your example it means:

``````Original value of lcd: B00010
Or mask (in setup):       B00100
------ OR
Result                    B00110
``````
• Why have we done this? `lcd |= B100;` .Where did the other elements go? – Black Thunder Jun 19 '19 at 9:21
• lcd will only change the 3th element, the other elements will remain equal. With the or (|) you can set a bit, all 0-values in an or 'mask' will remain unchanged. – Michel Keijzers Jun 19 '19 at 9:38
• Can I do directly like `lcd = B1;` where first 3 is the 4th row and second is the 3rd element? – Black Thunder Jun 19 '19 at 11:21
• No you cannot use lcd because lcd is one element (a byte) and not an array. – Michel Keijzers Jun 19 '19 at 12:03