问题
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 I feel a bit lost.
Pack and unpack variables into a byte . You need to store 4 different values in a byte. The values are:
NAME RANGE BITS engine_on 0-1 1 gear_pos 0-4 3 key_pos 0-2 2 brake1 0-1 1 brake2 0-1 1
(LSB, Least significant bit )
Write a program bytess.c that takes 5 arguments (less or more should be treated as an error). The arguments should correspond to the values/variables above. Example: bytess 1 2 2 1 1
The above should be treated as: Name Value engine_on 1 gear_pos 2 key_pos 2 brake1 1 brake2 1
Pack these values together in a byte (unsigned char) as an integer and print it out to stdout in hexadecimal form , in this example it should be ‘AB’ corresponding to bits ‘10101011. After this your program should return 0. If your program finds anything wrong (too many/few arguments, faulty input values.. ) your program should print error and return a not zero value.
I know how to check if those are 5 args, but I do not understand what to do next. Ive already read so much information about shifting but my brain can not handle the whole picture. It feels like this should be a really easy example, but I can not find anything on internet that would be similar. How does "AB" become 10101011? Ive just checked String to hex -> 4142. hex to bit -> 1000000101110.
回答1:
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
回答2:
Have you read about bit field?
struct s {
unsigned char engine_on : 1;
unsigned char gear_pos : 3;
unsigned char key_pos : 2;
unsigned char brake1 : 1;
unsigned char brake2 : 1;
};
回答3:
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);
来源:https://stackoverflow.com/questions/28507646/store-4-different-values-in-a-byte