I have an assignment to do, but I have no clue where to start. I am not expecting and definitely do not want answers in code. I would like some guidance as in what to do because
lets call a byte b, if you set b to 0, you end up with (binary) 0000 0000 (space for readability)
Now we want to pack the different parts into this byte
engine_on 0-1 1
gear_pos 0-4 3
key_pos 0-2 2
brake1 0-1 1
brake2 0-1 1
brake2 is simple. We can just set b to the value of brake2 and we will end up with 0000 0000, or 0000 0001 depending on if it is a 0 or a 1.
now we want to set brake 1 to b. We can do this by using a or/equal and the number itself but bitshifted to the right position. We end up with the following:
b |= (brake1 << 1)
lets explain how I came at this:
brake1 = 0000 0001 //lets assume its a 1 not a 0)
(brake1 << 1) = 0000 0010
b = 0000 0001 //lets assume brake 2 was 1.
to 'add' the value from brake1 to b we have to set each bit if either the bit in b is 1 or if the bit in (brake1 << 1) is one. This is done by a 'bitwise-or', so we end up with:
b = b | (brake1 << 1) // which can also be written as:
b |= (brake1 << 1)
now you can also add the other parts, it also works with more bits at the same time. I hope this has been helpful