method to get finger mask based on finger position
Clash 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;
c#
add a comment |Â
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;
c#
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
add a comment |Â
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;
c#
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;
c#
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
add a comment |Â
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
add a comment |Â
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;
add a comment |Â
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;
add a comment |Â
up vote
1
down vote
These magic numbers are very magical. These feel like they should be enum
s:
// 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.
add a comment |Â
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;
add a comment |Â
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;
add a comment |Â
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;
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;
answered Jan 4 at 16:04
Aaron M. Eshbach
3347
3347
add a comment |Â
add a comment |Â
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;
add a comment |Â
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;
add a comment |Â
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;
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;
answered Jan 4 at 16:20
Rick Davin
2,897618
2,897618
add a comment |Â
add a comment |Â
up vote
1
down vote
These magic numbers are very magical. These feel like they should be enum
s:
// 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.
add a comment |Â
up vote
1
down vote
These magic numbers are very magical. These feel like they should be enum
s:
// 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.
add a comment |Â
up vote
1
down vote
up vote
1
down vote
These magic numbers are very magical. These feel like they should be enum
s:
// 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.
These magic numbers are very magical. These feel like they should be enum
s:
// 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.
answered Jan 4 at 16:31
Jesse C. Slicer
10.9k2738
10.9k2738
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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