Big Number Bitwise Operator


/ Published in: Java
Save to your folder(s)

This Utility Class will receive two big number and do the Bitwise Operation on them using specific radix.


Copy this code and paste it in your HTML
  1. import java.math.BigInteger;
  2.  
  3. /**
  4.  * @author Mehdi
  5.  */
  6. public class Launcher {
  7. public static void main(String[] args) {
  8. System.out.println(BitwiseUtils.doBitwise("00101010", "00001111", 2, BitwiseUtils.BitwiseOperator.XOR));
  9. }
  10. }
  11.  
  12.  
  13. class BitwiseUtils {
  14. public enum BitwiseOperator {
  15. AND, OR, XOR
  16. }
  17.  
  18. public static String doBitwise(String number1, String number2, int radix, BitwiseOperator bitwiseOperator) {
  19. BigInteger bn1 = new BigInteger(number1, radix);
  20. BigInteger bn2 = new BigInteger(number2, radix);
  21. switch (bitwiseOperator) {
  22. case AND:
  23. return bn1.and(bn2).toString(radix);
  24. case OR:
  25. return bn1.or(bn2).toString(radix);
  26. case XOR:
  27. return bn1.xor(bn2).toString(radix);
  28.  
  29. }
  30.  
  31. return null;
  32. }
  33. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.