3
0
mirror of https://github.com/Qortal/qortal.git synced 2025-02-12 02:05:50 +00:00

Fix forging bit mask for account flags

This commit is contained in:
catbref 2019-09-13 11:47:04 +01:00
parent 8149e18f49
commit 158631e68a
2 changed files with 26 additions and 1 deletions

View File

@ -2,13 +2,14 @@ package org.qora.account;
import org.qora.block.BlockChain;
import org.qora.repository.DataException;
import org.qora.utils.BitTwiddling;
/** Relating to whether accounts can forge. */
public class Forging {
/** Returns mask for account flags for forging bits. */
public static int getForgingMask() {
return (1 << BlockChain.getInstance().getForgingTiers().size()) - 1;
return BitTwiddling.calcMask(BlockChain.getInstance().getForgingTiers().size() - 1);
}
public static boolean canForge(Account account) throws DataException {

View File

@ -0,0 +1,24 @@
package org.qora.utils;
public class BitTwiddling {
/**
* Returns bit-mask for values up to, and including, <tt>maxValue</tt>.
* <p>
* e.g. for values up to 5 (0101b) this returns a mask of 7 (0111b).
* <p>
* Based on Integer.highestOneBit.
*
* @param maxValue
* @return mask
*/
public static int calcMask(int maxValue) {
maxValue |= maxValue >> 1;
maxValue |= maxValue >> 2;
maxValue |= maxValue >> 4;
maxValue |= maxValue >> 8;
maxValue |= maxValue >> 16;
return maxValue;
}
}