Store 4 different values in a byte

前端 未结 3 1412
忘了有多久
忘了有多久 2021-01-25 01:32

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

3条回答
  •  一向
    一向 (楼主)
    2021-01-25 01:37

    Here is another way of doing it.

        unsigned int i = 0;
    
        engine_on = 1;
        gear_pos =2;
        key_pos = 2;
        brake1 = 1;
        brake2 = 1;
    
        i |= s1.brake2;
        i |= (s1.brake1 << 1);
        i |= (s1.key_pos << 2);
        i |= (s1.gear_pos << 4);
        i |= (s1.engine_on << 7);
    

提交回复
热议问题