Note that there are some explanatory texts on larger screens.

plurals
  1. POBit Operations with Enumerated Type
    text
    copied!<p>I'm having some trouble getting started with a lab of mine. The lab has the following instructions:</p> <blockquote> <p>Given a value <code>int input = 0b10010110</code> determine what sports an individual likes. Assume there will be no errors in this input. From left to right, each position represents the following: Football, Baseball, Volleyball, Swimming, Softball, Soccer, Hockey, Lacrosse.</p> <p>If there is a 1 is in that position, then the person likes that sport. Based on the “binary” input given, output to the screen all of the sports that the user enjoys. For the given binary number, the user likes, football, swimming, soccer and hockey.</p> <ul> <li><p>Do not make an array of characters.</p></li> <li><p>Be sure to use an enumerated data type.</p></li> </ul> </blockquote> <p>I'm not sure how I can compare each position of the string to tell whether it is a 1 or 0. One idea I have is to use an enumerated type where I set each sport to a ten digit number where only its appropriate position is 1</p> <pre><code>enum sport{ Football = 0010000000, Baseball = 0001000000, Volleyball = 0000100000, ... , Lacrosse = 0000000001 }; </code></pre> <p>I would then shift left/ right on the given value "input" the appropriate amount of times to leave only the specified position with its original value and to set all other values to 0. For Football:</p> <pre><code>input &lt;&lt; 2; input &gt;&gt; 9; input &lt;&lt; 7; </code></pre> <p>So the new set value would be <code>0010000000</code>. Then I'd be able to compare the number as a whole. I would have to that for each case, but I cannot think of a different way to do it.</p> <p>Am I completely off? Is there a more functional way to check the value of each position using bit operations? Thanks for any assistance in advance.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload