Click here to Skip to main content
15,884,353 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C++ can shift bits left or right, but contains no native functionality to rotate bits in an integer.
I... Each bit is shifted to the left. If the fifth bit is found to be one, we consider this an overflow and move it to the rightmost position, effectively ‘rotating’ the bits.

II... Note the use of binary literals. A prefix of 0b allows binary literals to be specified as zeroes and ones.

III... Note that a binary AND of value and OVERFLOW is compared to OVERFLOW to determine if a single bit matches regardless of the state of other bits. This technique, known as masking, will be required several times throughout your program.


5. Declare four integers (inValvesOpen, outValvesOpen, pistonUp, cylinderFire) and initialize to 4-bit binary literals as per the following cylinder table. Note that, in each variable, the leftmost bit represents cylinder 1 and the rightmost bit, cylinder

6. Create a for loop that executes five times. All remaining code should be implemented within this loop.

7. Declare six display strings that, when combined, represent the initial state of the ASCII art

8. Declare an inner loop, c, that executes 4 times, once for each cylinder, starting at zero (cylinder 1).
I.... Declare an integer offset and initialize it to the starting position of a specific cylinder in the display strings. (Each cylinder is nine characters wide.)

II... Declare six character variables that will be used to hold the character representing various changing parts of the cylinder.
III. Declare an integer positionMask and initialize for a single bit in the position representing cylinder 1.

i. To change the mask to represent the bit in the position of the current cylinder, shift it to the right by c.

IV. Use the position mask to determine the state of the current cylinder’s intake valve. If open (1), change the inValve character representation to ‘o’ and set the stateLabel to ‘I’ (intake)

V. Check the position of the piston using the same mask. If in the up position:

i. Change the upPiston character to show the piston (T) and the downPiston character to show an empty lower chamber (space character).

ii. If the cylinder is also firing, change the chamber value to an asterisk and the stateLabel to ‘P’ (power)

VI. Use the position mask to determine the state of the current cylinder’s exhaust valve. If open (1), change the outValve character representation to ‘o’ and set the stateLabel to ‘E’ (Exhaust).

VII. Change the characters within the display strings to match the state of these variables.


. Repeat for each of the four cylinders.

9. When all four cylinder states have been calculated, use the cout stream to display each of the six display strings on a separate line, followed by two blank lines.

10. To move to the next in firing order, perform the same binary rotation of each of the four state variables, depending on the current state of cylinderFire as follows:

I. Using bit masking, determine the number of bits to rotate to the left based on the currently firing cylinder.

II. If cylinderFire has bit 3 set (currently firing cylinder 3), a rotation of 1 bit to the left will make cylinder 2 the next cylinder to fire.

III. If currently firing cylinder 4, rotating 3 bits to the left will fire cylinder 1.

IV. Otherwise, if currently firing cylinders 1 or 2, rotating 2 bits to the left will fire cylinder 3 or 4 respectively.

V. Rotate each of the four binary state variables the same amount by calling the rotate function we created earlier.

VI. Repeat the same process five times, so we finish in the same state for each cylinder as we started.

11. Compare your working program to the output below and debug as necessary to achieve the same result. If working correctly, the output should look like this:

What I have tried:

no, i havent tried because i m unable to understand the question properly.
Posted
Updated 28-Mar-20 1:15am

If you cannot understand the question, then read (or re-read) your course material, which will contain elements relevant to the task which has been given to you.
If you still cannot see the link between your course and the question, then you should talk to your teacher.

Here are some links relevant to your task:
Bitwise operation - Wikipedia[^]
O.3 — Bit manipulation with bitwise operators and bit masks | Learn C++[^]
c - What is Bit Masking? - Stack Overflow[^]
 
Share this answer
 
Comments
Tomare UtsuZo 27-Jul-21 4:22am    
Hi. So, I like the above asker, don't understand this problem, though I can say the problem is conceptual, not a matter of not knowing how (what I don't already know how to do, I can work out how to do.)
This "
III. Declare an integer positionMask and initialize for a single bit in the position representing cylinder 1.

i. To change the mask to represent the bit in the position of the current cylinder, shift it to the right by c.
"
Is where I cam stuck right now. It' calls for my declaring an int, but, then it say's below shift it right by c. Shifting is done with binary numbers. What do them mean by, shift?
Please, don't ask us to do your own homework. Instead, try harder to do it yourself and post here only specific questions.
 
Share this answer
 
Comments
Michael Haephrati 6-Apr-20 17:29pm    
I entirely agree with what you wrote but you should have placed it as a comment. This is not an answer
Quote:
no, i havent tried because i m unable to understand the question properly.

We are more than willing to help those that are stuck: but that doesn't mean that we are here to do it all for you! We can't do all the work, you are either getting paid for this, or it's part of your grades and it wouldn't be at all fair for us to do it all for you.

So we need you to do the work, and we will help you when you get stuck. That doesn't mean we will give you a step by step solution you can hand in!
Start by explaining where you are at the moment, and what the next step in the process is. Then tell us what you have tried to get that next step working, and what happened when you did.

If you can't understand the question at all, then you need to talk to your tutor and find out why - this will be related to material that was covered recently, so either his explanation didn't work, or your comprehension failed. Either way, he needs to know, so he can correct it.
 
Share this answer
 
Quote:
no, i havent tried because i m unable to understand the question properly.

Reread your lessons or have a talk with your teacher.

We do not do your HomeWork.
Any failure of you will help your teacher spot your weaknesses and set remedial actions.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900