fork(1) download
  1. import java.math.BigInteger;
  2. import java.nio.ByteBuffer;
  3.  
  4. public class Main {
  5. public static void main (String[] args) throws java.lang.Exception {
  6. // c0 00
  7. Bytes bytes = new Bytes(new byte[] {(byte) 0xc0, (byte) 0x00});
  8. bytes.print();
  9.  
  10. // c0 00 を3インクリメント => c0 03
  11. bytes = bytes.increment(3);
  12. bytes.print();
  13.  
  14. // c0 03 & ff fe => c0 02
  15. bytes = bytes.and(new Bytes(new byte[] {(byte) 0xff, (byte) 0xfe}));
  16. bytes.print();
  17.  
  18. // c0 02 << 1 => 80 04
  19. bytes = bytes.shiftLeft(1);
  20. bytes.print();
  21. }
  22. }
  23.  
  24. class Bytes {
  25.  
  26. private final int byteSize;
  27. private final BigInteger mask;
  28. private final BigInteger value;
  29.  
  30. Bytes(byte[] byteArray) {
  31. this(byteArray, byteArray.length);
  32. }
  33.  
  34. private Bytes(byte[] byteArray, int byteSize) {
  35. this.byteSize = byteSize;
  36. ByteBuffer bb = ByteBuffer.allocate(byteSize);
  37. for(int i=0; i<byteSize; i++) {
  38. bb.put((byte) -1);
  39. }
  40. mask = new BigInteger(1, bb.array());
  41. value = new BigInteger(byteArray).and(mask);
  42. }
  43.  
  44. public Bytes increment(Integer bitNumber) {
  45. BigInteger newValue = value.add(new BigInteger(bitNumber.toString())).and(mask);
  46. return new Bytes(newValue.toByteArray(), byteSize);
  47. }
  48.  
  49. public Bytes decrement(Integer bitNumber) {
  50. BigInteger newValue = value.subtract(new BigInteger(bitNumber.toString())).and(mask);
  51. return new Bytes(newValue.toByteArray(), byteSize);
  52. }
  53.  
  54. public Bytes shiftRight(Integer bitNumber) {
  55. BigInteger newValue = value.shiftRight(bitNumber).and(mask);
  56. return new Bytes(newValue.toByteArray(), byteSize);
  57. }
  58.  
  59. public Bytes shiftLeft(Integer bitNumber) {
  60. BigInteger newValue = value.shiftLeft(bitNumber).and(mask);
  61. return new Bytes(newValue.toByteArray(), byteSize);
  62. }
  63.  
  64. public Bytes and(Bytes operand) {
  65. BigInteger newValue = value.and(operand.value).and(mask);
  66. return new Bytes(newValue.toByteArray(), byteSize);
  67. }
  68.  
  69. public Bytes or(Bytes operand) {
  70. BigInteger newValue = value.or(operand.value).and(mask);
  71. return new Bytes(newValue.toByteArray(), byteSize);
  72. }
  73.  
  74. public Bytes xor(Bytes operand) {
  75. BigInteger newValue = value.xor(operand.value).and(mask);
  76. return new Bytes(newValue.toByteArray(), byteSize);
  77. }
  78.  
  79. public Bytes not() {
  80. BigInteger newValue = value.not().and(mask);
  81. return new Bytes(newValue.toByteArray(), byteSize);
  82. }
  83.  
  84. /** BigIntegerを固定長のbyte配列に変換する。 */
  85. public byte[] toByteArray() {
  86. byte[] ba = value.toByteArray();
  87. ByteBuffer bb = ByteBuffer.allocate(byteSize);
  88.  
  89. // 余分な上位バイトを取り除く。
  90. if (ba.length >= byteSize) {
  91. bb.put(ba, ba.length-byteSize, byteSize);
  92.  
  93. // 不足するbyte数分ByteBufferを先頭から埋める。
  94. } else {
  95. int byteSizeToFill = byteSize - ba.length;
  96. for(int i=0; i<byteSizeToFill; i++) {
  97. bb.put((byte) 0);
  98. }
  99. bb.put(ba);
  100. }
  101.  
  102. return bb.array();
  103. }
  104.  
  105. /** byte配列を16進数表記で1バイトずつ表示する。 */
  106. public void print() {
  107. StringBuilder sb = new StringBuilder();
  108. for(byte b : this.toByteArray()) {
  109. sb.append(String.format("%02x ", b));
  110. }
  111. System.out.println(sb.toString());
  112. }
  113. }
Success #stdin #stdout 0.1s 28228KB
stdin
Standard input is empty
stdout
c0 00 
c0 03 
c0 02 
80 04