Skip to main content
Arduino

Return to Answer

replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link

For the general case, @dat_ha 's answer is correct, but it is worth noting that you want a very special case... powers of two. Because computers use binary arithmetic, operations involving powers of two often have some shortcuts available.

Multiplying a number by a power of two can be accomplished by the left shift operation (<<), which literally shifts the digits of the binary representation of the number (i.e., bits) leftward. In base two, shifting bits one place to the left is the same as multiplying by 2, just as in base 10 shifting digits one place to the left is the same as multiplying by 10. For a full explanation of the left shift operator in C++, see this answer on Stack Overflow this answer on Stack Overflow.

It is important to note that left shifting can lose information; bits shifted off the end are lost. Because you need powers of 2 up to 10, you are safe when working with signed integers, which have a max value of 2^15-1 on Arduino Uno.

Given those caveats, here's a function to calculate powers of two within these constraints. This is very fast code because the left shift operation is a very low level operation, and no multiplication is actually performed.

int pow2(int p){
 return 1 << p;
}

For the general case, @dat_ha 's answer is correct, but it is worth noting that you want a very special case... powers of two. Because computers use binary arithmetic, operations involving powers of two often have some shortcuts available.

Multiplying a number by a power of two can be accomplished by the left shift operation (<<), which literally shifts the digits of the binary representation of the number (i.e., bits) leftward. In base two, shifting bits one place to the left is the same as multiplying by 2, just as in base 10 shifting digits one place to the left is the same as multiplying by 10. For a full explanation of the left shift operator in C++, see this answer on Stack Overflow.

It is important to note that left shifting can lose information; bits shifted off the end are lost. Because you need powers of 2 up to 10, you are safe when working with signed integers, which have a max value of 2^15-1 on Arduino Uno.

Given those caveats, here's a function to calculate powers of two within these constraints. This is very fast code because the left shift operation is a very low level operation, and no multiplication is actually performed.

int pow2(int p){
 return 1 << p;
}

For the general case, @dat_ha 's answer is correct, but it is worth noting that you want a very special case... powers of two. Because computers use binary arithmetic, operations involving powers of two often have some shortcuts available.

Multiplying a number by a power of two can be accomplished by the left shift operation (<<), which literally shifts the digits of the binary representation of the number (i.e., bits) leftward. In base two, shifting bits one place to the left is the same as multiplying by 2, just as in base 10 shifting digits one place to the left is the same as multiplying by 10. For a full explanation of the left shift operator in C++, see this answer on Stack Overflow.

It is important to note that left shifting can lose information; bits shifted off the end are lost. Because you need powers of 2 up to 10, you are safe when working with signed integers, which have a max value of 2^15-1 on Arduino Uno.

Given those caveats, here's a function to calculate powers of two within these constraints. This is very fast code because the left shift operation is a very low level operation, and no multiplication is actually performed.

int pow2(int p){
 return 1 << p;
}
Fix off-by-one error.
Source Link
Edgar Bonet
  • 45.1k
  • 4
  • 42
  • 81

For the general case, @dat_ha 's answer is correct, but it is worth noting that you want a very special case... powers of two. Because computers use binary arithmetic, operations involving powers of two often have some shortcuts available.

Multiplying a number by a power of two can be accomplished by the left shift operation (<<), which literally shifts the digits of the binary representation of the number (i.e., bits) leftward. In base two, shifting bits one place to the left is the same as multiplying by 2, just as in base 10 shifting digits one place to the left is the same as multiplying by 10. For a full explanation of the left shift operator in C++, see this answer on Stack Overflow.

It is important to note that left shifting can lose information; bits shifted off the end are lost. Because you need powers of 2 up to 10, you are safe when working with signed integers, which have a max value of 2^15-1 on Arduino Uno.

Given those caveats, here's a function to calculate powers of two within these constraints. This is very fast code because the left shift operation is a very low level operation, and no multiplication is actually performed.

int pow2(int p){
 return 1 << p;
}

For the general case, @dat_ha 's answer is correct, but it is worth noting that you want a very special case... powers of two. Because computers use binary arithmetic, operations involving powers of two often have some shortcuts available.

Multiplying a number by a power of two can be accomplished by the left shift operation (<<), which literally shifts the digits of the binary representation of the number (i.e., bits) leftward. In base two, shifting bits one place to the left is the same as multiplying by 2, just as in base 10 shifting digits one place to the left is the same as multiplying by 10. For a full explanation of the left shift operator in C++, see this answer on Stack Overflow.

It is important to note that left shifting can lose information; bits shifted off the end are lost. Because you need powers of 2 up to 10, you are safe when working with signed integers, which have a max value of 2^15 on Arduino Uno.

Given those caveats, here's a function to calculate powers of two within these constraints. This is very fast code because the left shift operation is a very low level operation, and no multiplication is actually performed.

int pow2(int p){
 return 1 << p;
}

For the general case, @dat_ha 's answer is correct, but it is worth noting that you want a very special case... powers of two. Because computers use binary arithmetic, operations involving powers of two often have some shortcuts available.

Multiplying a number by a power of two can be accomplished by the left shift operation (<<), which literally shifts the digits of the binary representation of the number (i.e., bits) leftward. In base two, shifting bits one place to the left is the same as multiplying by 2, just as in base 10 shifting digits one place to the left is the same as multiplying by 10. For a full explanation of the left shift operator in C++, see this answer on Stack Overflow.

It is important to note that left shifting can lose information; bits shifted off the end are lost. Because you need powers of 2 up to 10, you are safe when working with signed integers, which have a max value of 2^15-1 on Arduino Uno.

Given those caveats, here's a function to calculate powers of two within these constraints. This is very fast code because the left shift operation is a very low level operation, and no multiplication is actually performed.

int pow2(int p){
 return 1 << p;
}
added 7 characters in body
Source Link

For the general case, @dat_ha 's answer is correct, but it is worth noting that you want a very special case... powers of two. Because computers use binary arithmetic, operations involving powers of two often have some shortcuts available.

Multiplying a number by a power of two can be accomplished by the left shift operation (<<), which literally shifts the digits of the binary representation of the number (i.e., bits) leftward. In base two, shifting bits one place to the left is the same as multiplying by 2, just as in base 10 shifting digits one place to the left is the same as multiplying by 10. For a full explanation of the left shift operator in C++, see this answer on Stack Overflow.

It is important to note that left shifting can lose information; bits shifted off the end are lost. Because you need powers of 2 up to 10, you are safe when working with signed integers, which have a max value of 2^15 on Arduino Uno.

Given those caveats, here's a function to calculate powers of two within these constraints. This is very fast code because the left shift operation is a very low level operation, and no multiplication is actually performed.

int pow2(int p){
 return 1 << p;
}

For the general case, @dat_ha 's answer is correct, but it is worth noting that you want a very special case... powers of two. Because computers use binary arithmetic, operations involving powers of two often have some shortcuts available.

Multiplying a number by a power of two can be accomplished by the left shift operation (<<), which literally shifts the digits of the binary representation of the number (i.e., bits) leftward. In base two, shifting bits one place to the left is the same as multiplying by 2, just as in base 10 shifting digits one place to the left is the same as multiplying by 10. For a full explanation of the left shift operator in C++, see this answer on Stack Overflow.

It is important to note that left shifting can lose information; bits shifted off the end are lost. Because you need powers of 2 up to 10, you are safe when working with integers, which have a max value of 2^15 on Arduino Uno.

Given those caveats, here's a function to calculate powers of two within these constraints. This is very fast code because the left shift operation is a very low level operation, and no multiplication is actually performed.

int pow2(int p){
 return 1 << p;
}

For the general case, @dat_ha 's answer is correct, but it is worth noting that you want a very special case... powers of two. Because computers use binary arithmetic, operations involving powers of two often have some shortcuts available.

Multiplying a number by a power of two can be accomplished by the left shift operation (<<), which literally shifts the digits of the binary representation of the number (i.e., bits) leftward. In base two, shifting bits one place to the left is the same as multiplying by 2, just as in base 10 shifting digits one place to the left is the same as multiplying by 10. For a full explanation of the left shift operator in C++, see this answer on Stack Overflow.

It is important to note that left shifting can lose information; bits shifted off the end are lost. Because you need powers of 2 up to 10, you are safe when working with signed integers, which have a max value of 2^15 on Arduino Uno.

Given those caveats, here's a function to calculate powers of two within these constraints. This is very fast code because the left shift operation is a very low level operation, and no multiplication is actually performed.

int pow2(int p){
 return 1 << p;
}
Source Link
Loading

AltStyle によって変換されたページ (->オリジナル) /