method to get finger mask based on finger position

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
2
down vote

favorite












Is there a more graceful way to structure the code below?



The fingerPosition values are increments of 1.



The fingerMask values start at 1, then 2 and then increment in multiples of 2.



 private int getFingerMask(int fingerPosition)

int fingerMask = 0;

if (fingerPosition == 0)

fingerMask = 1;

else if (fingerPosition == 1)

fingerMask = 2;

else if (fingerPosition == 2)

fingerMask = 4;

else if (fingerPosition == 3)

fingerMask = 8;

else if (fingerPosition == 4)

fingerMask = 16;

else if (fingerPosition == 5)

fingerMask = 32;

else if (fingerPosition == 6)

fingerMask = 64;

else if (fingerPosition == 7)

fingerMask = 128;

else if (fingerPosition == 8)

fingerMask = 256;

else if (fingerPosition == 9)

fingerMask = 512;


return fingerMask;







share|improve this question

















  • 1




    Welcome to Code Review! I changed the title so that it describes what the code does per site goals: "State what your code does in your title, not your main concerns about it.". Feel free to give it a different title if there is something more appropriate.
    – Sam Onela
    Jan 4 at 16:42
















up vote
2
down vote

favorite












Is there a more graceful way to structure the code below?



The fingerPosition values are increments of 1.



The fingerMask values start at 1, then 2 and then increment in multiples of 2.



 private int getFingerMask(int fingerPosition)

int fingerMask = 0;

if (fingerPosition == 0)

fingerMask = 1;

else if (fingerPosition == 1)

fingerMask = 2;

else if (fingerPosition == 2)

fingerMask = 4;

else if (fingerPosition == 3)

fingerMask = 8;

else if (fingerPosition == 4)

fingerMask = 16;

else if (fingerPosition == 5)

fingerMask = 32;

else if (fingerPosition == 6)

fingerMask = 64;

else if (fingerPosition == 7)

fingerMask = 128;

else if (fingerPosition == 8)

fingerMask = 256;

else if (fingerPosition == 9)

fingerMask = 512;


return fingerMask;







share|improve this question

















  • 1




    Welcome to Code Review! I changed the title so that it describes what the code does per site goals: "State what your code does in your title, not your main concerns about it.". Feel free to give it a different title if there is something more appropriate.
    – Sam Onela
    Jan 4 at 16:42












up vote
2
down vote

favorite









up vote
2
down vote

favorite











Is there a more graceful way to structure the code below?



The fingerPosition values are increments of 1.



The fingerMask values start at 1, then 2 and then increment in multiples of 2.



 private int getFingerMask(int fingerPosition)

int fingerMask = 0;

if (fingerPosition == 0)

fingerMask = 1;

else if (fingerPosition == 1)

fingerMask = 2;

else if (fingerPosition == 2)

fingerMask = 4;

else if (fingerPosition == 3)

fingerMask = 8;

else if (fingerPosition == 4)

fingerMask = 16;

else if (fingerPosition == 5)

fingerMask = 32;

else if (fingerPosition == 6)

fingerMask = 64;

else if (fingerPosition == 7)

fingerMask = 128;

else if (fingerPosition == 8)

fingerMask = 256;

else if (fingerPosition == 9)

fingerMask = 512;


return fingerMask;







share|improve this question













Is there a more graceful way to structure the code below?



The fingerPosition values are increments of 1.



The fingerMask values start at 1, then 2 and then increment in multiples of 2.



 private int getFingerMask(int fingerPosition)

int fingerMask = 0;

if (fingerPosition == 0)

fingerMask = 1;

else if (fingerPosition == 1)

fingerMask = 2;

else if (fingerPosition == 2)

fingerMask = 4;

else if (fingerPosition == 3)

fingerMask = 8;

else if (fingerPosition == 4)

fingerMask = 16;

else if (fingerPosition == 5)

fingerMask = 32;

else if (fingerPosition == 6)

fingerMask = 64;

else if (fingerPosition == 7)

fingerMask = 128;

else if (fingerPosition == 8)

fingerMask = 256;

else if (fingerPosition == 9)

fingerMask = 512;


return fingerMask;









share|improve this question












share|improve this question




share|improve this question








edited Jan 4 at 16:41









Sam Onela

5,88461545




5,88461545









asked Jan 4 at 15:56









testytest

132




132







  • 1




    Welcome to Code Review! I changed the title so that it describes what the code does per site goals: "State what your code does in your title, not your main concerns about it.". Feel free to give it a different title if there is something more appropriate.
    – Sam Onela
    Jan 4 at 16:42












  • 1




    Welcome to Code Review! I changed the title so that it describes what the code does per site goals: "State what your code does in your title, not your main concerns about it.". Feel free to give it a different title if there is something more appropriate.
    – Sam Onela
    Jan 4 at 16:42







1




1




Welcome to Code Review! I changed the title so that it describes what the code does per site goals: "State what your code does in your title, not your main concerns about it.". Feel free to give it a different title if there is something more appropriate.
– Sam Onela
Jan 4 at 16:42




Welcome to Code Review! I changed the title so that it describes what the code does per site goals: "State what your code does in your title, not your main concerns about it.". Feel free to give it a different title if there is something more appropriate.
– Sam Onela
Jan 4 at 16:42










3 Answers
3






active

oldest

votes

















up vote
2
down vote



accepted










Yes, this can be done simply by using a power of 2.



private int getFingerMask(int fingerPosition)

if (fingerPosition >= 0 && fingerPosition < 10)

return (int)Math.Pow(2, fingerPosition);

else

return 0;







share|improve this answer




























    up vote
    1
    down vote













    In addition to @Aaron's answer, you can also use bit shifting. Also Naming Guidelines would expect the method to begin with a capital letter. This could also be reduced to a one-line method:



    private int GetFingerMask(int fingerPosition) => (fingerPosition >= 0 && fingerPosition < 10) ? 1 << fingerPosition : 0;





    share|improve this answer




























      up vote
      1
      down vote













      These magic numbers are very magical. These feel like they should be enums:



      // Perhaps there are better names for these positions? Thumb, Forefinger, etc.?
      public enum FingerPosition

      Zero = 0,
      One,
      Two,
      Three,
      Four,
      Five,
      Six,
      Seven,
      Eight,
      Nine


      // Again, better naming would be in order.
      [Flags]
      public enum FingerMask

      Unknown = 0,
      Zero = 1,
      One = 2,
      Two = 4,
      Three = 8,
      Four = 16,
      Five = 32,
      Six = 64,
      Seven = 128,
      Eight = 256,
      Nine = 512



      Then something like a Dictionary to associate them as such:



      private static readonly IDictionary<FingerPosition, FingerMask> _FingerMap = new Dictionary<FingerPosition, FingerMask>

      [FingerPosition.Zero] = FingerMask.Zero,
      [FingerPosition.One] = FingerMask.One,
      [FingerPosition.Two] = FingerMask.Two,
      [FingerPosition.Three] = FingerMask.Three,
      [FingerPosition.Four] = FingerMask.Four,
      [FingerPosition.Five] = FingerMask.Five,
      [FingerPosition.Six] = FingerMask.Six,
      [FingerPosition.Seven] = FingerMask.Seven,
      [FingerPosition.Eight] = FingerMask.Eight,
      [FingerPosition.Nine] = FingerMask.Nine,
      ;


      Your code then becomes type-safe in this manner:



      private FingerMask getFingerMask(FingerPosition fingerPosition)

      FingerMask fingerMask;

      if (_FingerMap.TryGetValue(fingerPosition, out fingerMask))

      return fingerMask;


      return FingerMask.Unknown;



      You can then cast that result to int if you really need it somewhere else as that.






      share|improve this answer





















        Your Answer




        StackExchange.ifUsing("editor", function ()
        return StackExchange.using("mathjaxEditing", function ()
        StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
        StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
        );
        );
        , "mathjax-editing");

        StackExchange.ifUsing("editor", function ()
        StackExchange.using("externalEditor", function ()
        StackExchange.using("snippets", function ()
        StackExchange.snippets.init();
        );
        );
        , "code-snippets");

        StackExchange.ready(function()
        var channelOptions =
        tags: "".split(" "),
        id: "196"
        ;
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function()
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled)
        StackExchange.using("snippets", function()
        createEditor();
        );

        else
        createEditor();

        );

        function createEditor()
        StackExchange.prepareEditor(
        heartbeatType: 'answer',
        convertImagesToLinks: false,
        noModals: false,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: null,
        bindNavPrevention: true,
        postfix: "",
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        );



        );








         

        draft saved


        draft discarded


















        StackExchange.ready(
        function ()
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f184287%2fmethod-to-get-finger-mask-based-on-finger-position%23new-answer', 'question_page');

        );

        Post as a guest






























        3 Answers
        3






        active

        oldest

        votes








        3 Answers
        3






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes








        up vote
        2
        down vote



        accepted










        Yes, this can be done simply by using a power of 2.



        private int getFingerMask(int fingerPosition)

        if (fingerPosition >= 0 && fingerPosition < 10)

        return (int)Math.Pow(2, fingerPosition);

        else

        return 0;







        share|improve this answer

























          up vote
          2
          down vote



          accepted










          Yes, this can be done simply by using a power of 2.



          private int getFingerMask(int fingerPosition)

          if (fingerPosition >= 0 && fingerPosition < 10)

          return (int)Math.Pow(2, fingerPosition);

          else

          return 0;







          share|improve this answer























            up vote
            2
            down vote



            accepted







            up vote
            2
            down vote



            accepted






            Yes, this can be done simply by using a power of 2.



            private int getFingerMask(int fingerPosition)

            if (fingerPosition >= 0 && fingerPosition < 10)

            return (int)Math.Pow(2, fingerPosition);

            else

            return 0;







            share|improve this answer













            Yes, this can be done simply by using a power of 2.



            private int getFingerMask(int fingerPosition)

            if (fingerPosition >= 0 && fingerPosition < 10)

            return (int)Math.Pow(2, fingerPosition);

            else

            return 0;








            share|improve this answer













            share|improve this answer



            share|improve this answer











            answered Jan 4 at 16:04









            Aaron M. Eshbach

            3347




            3347






















                up vote
                1
                down vote













                In addition to @Aaron's answer, you can also use bit shifting. Also Naming Guidelines would expect the method to begin with a capital letter. This could also be reduced to a one-line method:



                private int GetFingerMask(int fingerPosition) => (fingerPosition >= 0 && fingerPosition < 10) ? 1 << fingerPosition : 0;





                share|improve this answer

























                  up vote
                  1
                  down vote













                  In addition to @Aaron's answer, you can also use bit shifting. Also Naming Guidelines would expect the method to begin with a capital letter. This could also be reduced to a one-line method:



                  private int GetFingerMask(int fingerPosition) => (fingerPosition >= 0 && fingerPosition < 10) ? 1 << fingerPosition : 0;





                  share|improve this answer























                    up vote
                    1
                    down vote










                    up vote
                    1
                    down vote









                    In addition to @Aaron's answer, you can also use bit shifting. Also Naming Guidelines would expect the method to begin with a capital letter. This could also be reduced to a one-line method:



                    private int GetFingerMask(int fingerPosition) => (fingerPosition >= 0 && fingerPosition < 10) ? 1 << fingerPosition : 0;





                    share|improve this answer













                    In addition to @Aaron's answer, you can also use bit shifting. Also Naming Guidelines would expect the method to begin with a capital letter. This could also be reduced to a one-line method:



                    private int GetFingerMask(int fingerPosition) => (fingerPosition >= 0 && fingerPosition < 10) ? 1 << fingerPosition : 0;






                    share|improve this answer













                    share|improve this answer



                    share|improve this answer











                    answered Jan 4 at 16:20









                    Rick Davin

                    2,897618




                    2,897618




















                        up vote
                        1
                        down vote













                        These magic numbers are very magical. These feel like they should be enums:



                        // Perhaps there are better names for these positions? Thumb, Forefinger, etc.?
                        public enum FingerPosition

                        Zero = 0,
                        One,
                        Two,
                        Three,
                        Four,
                        Five,
                        Six,
                        Seven,
                        Eight,
                        Nine


                        // Again, better naming would be in order.
                        [Flags]
                        public enum FingerMask

                        Unknown = 0,
                        Zero = 1,
                        One = 2,
                        Two = 4,
                        Three = 8,
                        Four = 16,
                        Five = 32,
                        Six = 64,
                        Seven = 128,
                        Eight = 256,
                        Nine = 512



                        Then something like a Dictionary to associate them as such:



                        private static readonly IDictionary<FingerPosition, FingerMask> _FingerMap = new Dictionary<FingerPosition, FingerMask>

                        [FingerPosition.Zero] = FingerMask.Zero,
                        [FingerPosition.One] = FingerMask.One,
                        [FingerPosition.Two] = FingerMask.Two,
                        [FingerPosition.Three] = FingerMask.Three,
                        [FingerPosition.Four] = FingerMask.Four,
                        [FingerPosition.Five] = FingerMask.Five,
                        [FingerPosition.Six] = FingerMask.Six,
                        [FingerPosition.Seven] = FingerMask.Seven,
                        [FingerPosition.Eight] = FingerMask.Eight,
                        [FingerPosition.Nine] = FingerMask.Nine,
                        ;


                        Your code then becomes type-safe in this manner:



                        private FingerMask getFingerMask(FingerPosition fingerPosition)

                        FingerMask fingerMask;

                        if (_FingerMap.TryGetValue(fingerPosition, out fingerMask))

                        return fingerMask;


                        return FingerMask.Unknown;



                        You can then cast that result to int if you really need it somewhere else as that.






                        share|improve this answer

























                          up vote
                          1
                          down vote













                          These magic numbers are very magical. These feel like they should be enums:



                          // Perhaps there are better names for these positions? Thumb, Forefinger, etc.?
                          public enum FingerPosition

                          Zero = 0,
                          One,
                          Two,
                          Three,
                          Four,
                          Five,
                          Six,
                          Seven,
                          Eight,
                          Nine


                          // Again, better naming would be in order.
                          [Flags]
                          public enum FingerMask

                          Unknown = 0,
                          Zero = 1,
                          One = 2,
                          Two = 4,
                          Three = 8,
                          Four = 16,
                          Five = 32,
                          Six = 64,
                          Seven = 128,
                          Eight = 256,
                          Nine = 512



                          Then something like a Dictionary to associate them as such:



                          private static readonly IDictionary<FingerPosition, FingerMask> _FingerMap = new Dictionary<FingerPosition, FingerMask>

                          [FingerPosition.Zero] = FingerMask.Zero,
                          [FingerPosition.One] = FingerMask.One,
                          [FingerPosition.Two] = FingerMask.Two,
                          [FingerPosition.Three] = FingerMask.Three,
                          [FingerPosition.Four] = FingerMask.Four,
                          [FingerPosition.Five] = FingerMask.Five,
                          [FingerPosition.Six] = FingerMask.Six,
                          [FingerPosition.Seven] = FingerMask.Seven,
                          [FingerPosition.Eight] = FingerMask.Eight,
                          [FingerPosition.Nine] = FingerMask.Nine,
                          ;


                          Your code then becomes type-safe in this manner:



                          private FingerMask getFingerMask(FingerPosition fingerPosition)

                          FingerMask fingerMask;

                          if (_FingerMap.TryGetValue(fingerPosition, out fingerMask))

                          return fingerMask;


                          return FingerMask.Unknown;



                          You can then cast that result to int if you really need it somewhere else as that.






                          share|improve this answer























                            up vote
                            1
                            down vote










                            up vote
                            1
                            down vote









                            These magic numbers are very magical. These feel like they should be enums:



                            // Perhaps there are better names for these positions? Thumb, Forefinger, etc.?
                            public enum FingerPosition

                            Zero = 0,
                            One,
                            Two,
                            Three,
                            Four,
                            Five,
                            Six,
                            Seven,
                            Eight,
                            Nine


                            // Again, better naming would be in order.
                            [Flags]
                            public enum FingerMask

                            Unknown = 0,
                            Zero = 1,
                            One = 2,
                            Two = 4,
                            Three = 8,
                            Four = 16,
                            Five = 32,
                            Six = 64,
                            Seven = 128,
                            Eight = 256,
                            Nine = 512



                            Then something like a Dictionary to associate them as such:



                            private static readonly IDictionary<FingerPosition, FingerMask> _FingerMap = new Dictionary<FingerPosition, FingerMask>

                            [FingerPosition.Zero] = FingerMask.Zero,
                            [FingerPosition.One] = FingerMask.One,
                            [FingerPosition.Two] = FingerMask.Two,
                            [FingerPosition.Three] = FingerMask.Three,
                            [FingerPosition.Four] = FingerMask.Four,
                            [FingerPosition.Five] = FingerMask.Five,
                            [FingerPosition.Six] = FingerMask.Six,
                            [FingerPosition.Seven] = FingerMask.Seven,
                            [FingerPosition.Eight] = FingerMask.Eight,
                            [FingerPosition.Nine] = FingerMask.Nine,
                            ;


                            Your code then becomes type-safe in this manner:



                            private FingerMask getFingerMask(FingerPosition fingerPosition)

                            FingerMask fingerMask;

                            if (_FingerMap.TryGetValue(fingerPosition, out fingerMask))

                            return fingerMask;


                            return FingerMask.Unknown;



                            You can then cast that result to int if you really need it somewhere else as that.






                            share|improve this answer













                            These magic numbers are very magical. These feel like they should be enums:



                            // Perhaps there are better names for these positions? Thumb, Forefinger, etc.?
                            public enum FingerPosition

                            Zero = 0,
                            One,
                            Two,
                            Three,
                            Four,
                            Five,
                            Six,
                            Seven,
                            Eight,
                            Nine


                            // Again, better naming would be in order.
                            [Flags]
                            public enum FingerMask

                            Unknown = 0,
                            Zero = 1,
                            One = 2,
                            Two = 4,
                            Three = 8,
                            Four = 16,
                            Five = 32,
                            Six = 64,
                            Seven = 128,
                            Eight = 256,
                            Nine = 512



                            Then something like a Dictionary to associate them as such:



                            private static readonly IDictionary<FingerPosition, FingerMask> _FingerMap = new Dictionary<FingerPosition, FingerMask>

                            [FingerPosition.Zero] = FingerMask.Zero,
                            [FingerPosition.One] = FingerMask.One,
                            [FingerPosition.Two] = FingerMask.Two,
                            [FingerPosition.Three] = FingerMask.Three,
                            [FingerPosition.Four] = FingerMask.Four,
                            [FingerPosition.Five] = FingerMask.Five,
                            [FingerPosition.Six] = FingerMask.Six,
                            [FingerPosition.Seven] = FingerMask.Seven,
                            [FingerPosition.Eight] = FingerMask.Eight,
                            [FingerPosition.Nine] = FingerMask.Nine,
                            ;


                            Your code then becomes type-safe in this manner:



                            private FingerMask getFingerMask(FingerPosition fingerPosition)

                            FingerMask fingerMask;

                            if (_FingerMap.TryGetValue(fingerPosition, out fingerMask))

                            return fingerMask;


                            return FingerMask.Unknown;



                            You can then cast that result to int if you really need it somewhere else as that.







                            share|improve this answer













                            share|improve this answer



                            share|improve this answer











                            answered Jan 4 at 16:31









                            Jesse C. Slicer

                            10.9k2738




                            10.9k2738






















                                 

                                draft saved


                                draft discarded


























                                 


                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function ()
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f184287%2fmethod-to-get-finger-mask-based-on-finger-position%23new-answer', 'question_page');

                                );

                                Post as a guest













































































                                Popular posts from this blog

                                Greedy Best First Search implementation in Rust

                                Function to Return a JSON Like Objects Using VBA Collections and Arrays

                                C++11 CLH Lock Implementation