GlueGen v2.6.0-rc-20250712
GlueGen, Native Binding Generator for Java™ (public API).
BitDemoData.java
Go to the documentation of this file.
1/**
2 * Copyright 2014 JogAmp Community. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without modification, are
5 * permitted provided that the following conditions are met:
6 *
7 * 1. Redistributions of source code must retain the above copyright notice, this list of
8 * conditions and the following disclaimer.
9 *
10 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
11 * of conditions and the following disclaimer in the documentation and/or other materials
12 * provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY JogAmp Community ``AS IS'' AND ANY EXPRESS OR IMPLIED
15 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
16 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JogAmp Community OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
19 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
20 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
21 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
22 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 *
24 * The views and conclusions contained in the software and documentation are those of the
25 * authors and should not be interpreted as representing official policies, either expressed
26 * or implied, of JogAmp Community.
27 */
28
29package com.jogamp.common.util;
30
31import java.nio.ByteBuffer;
32import java.util.Locale;
33
34public class BitDemoData {
35 public static final long UNSIGNED_INT_MAX_VALUE = 0xffffffffL;
36
37 public static final String[] pyramid32bit_one = {
38 "00000000000000000000000000000001",
39 "00000000000000000000000000000010",
40 "00000000000000000000000000000100",
41 "00000000000000000000000000001000",
42 "00000000000000000000000000010000",
43 "00000000000000000000000000100000",
44 "00000000000000000000000001000000",
45 "00000000000000000000000010000000",
46 "00000000000000000000000100000000",
47 "00000000000000000000001000000000",
48 "00000000000000000000010000000000",
49 "00000000000000000000100000000000",
50 "00000000000000000001000000000000",
51 "00000000000000000010000000000000",
52 "00000000000000000100000000000000",
53 "00000000000000001000000000000000",
54 "00000000000000010000000000000000",
55 "00000000000000100000000000000000",
56 "00000000000001000000000000000000",
57 "00000000000010000000000000000000",
58 "00000000000100000000000000000000",
59 "00000000001000000000000000000000",
60 "00000000010000000000000000000000",
61 "00000000100000000000000000000000",
62 "00000001000000000000000000000000",
63 "00000010000000000000000000000000",
64 "00000100000000000000000000000000",
65 "00001000000000000000000000000000",
66 "00010000000000000000000000000000",
67 "00100000000000000000000000000000",
68 "01000000000000000000000000000000",
69 "10000000000000000000000000000000"
70 };
71
72 //
73 // MSB -> LSB over whole data
74 //
75 public static final byte[] testBytesMSB = new byte[] { (byte)0xde, (byte)0xaf, (byte)0xca, (byte)0xfe };
76 public static final int testIntMSB = 0xdeafcafe; // 11011110 10101111 11001010 11111110
77 public static final String[] testStringsMSB = new String[] { "11011110", "10101111", "11001010", "11111110" };
79
80 //
81 // MSB -> LSB, reverse bit-order over each byte of testBytesLSB
82 //
83 public static final byte[] testBytesMSB_rev = new byte[] { (byte)0xfe, (byte)0xca, (byte)0xaf, (byte)0xde };
84 public static final int testIntMSB_rev = 0xfecaafde;
85 public static final String[] testStringsMSB_rev = new String[] { "11111110", "11001010", "10101111", "11011110" };
87
88 //
89 // LSB -> MSB over whole data
90 //
91 public static final byte[] testBytesLSB = new byte[] { (byte)0x7f, (byte)0x53, (byte)0xf5, (byte)0x7b };
92 public static final int testIntLSB = 0x7f53f57b;
93 public static final String[] testStringsLSB = new String[] { "01111111", "01010011", "11110101", "01111011" };
95
96 //
97 // LSB -> MSB, reverse bit-order over each byte of testBytesMSB
98 //
99 public static final byte[] testBytesLSB_revByte = new byte[] { (byte)0x7b, (byte)0xf5, (byte)0x53, (byte)0x7f };
100 public static final int testIntLSB_revByte = 0x7bf5537f;
101 public static final String[] testStringsLSB_revByte = new String[] { "01111011", "11110101", "01010011", "01111111" };
103
104 public static final void dumpData(final String prefix, final byte[] data, final int offset, final int len) {
105 for(int i=0; i<len; ) {
106 System.err.printf("%s: %03d: ", prefix, i);
107 for(int j=0; j<8 && i<len; j++, i++) {
108 final int v = 0xFF & data[offset+i];
109 System.err.printf(toHexBinaryString(v, 8)+", ");
110 }
111 System.err.println("");
112 }
113 }
114 public static final void dumpData(final String prefix, final ByteBuffer data, final int offset, final int len) {
115 for(int i=0; i<len; ) {
116 System.err.printf("%s: %03d: ", prefix, i);
117 for(int j=0; j<8 && i<len; j++, i++) {
118 final int v = 0xFF & data.get(offset+i);
119 System.err.printf(toHexBinaryString(v, 8)+", ");
120 }
121 System.err.println("");
122 }
123 }
124
125 public static int getOneBitCount(final String pattern) {
126 int c=0;
127 for(int i=0; i<pattern.length(); i++) {
128 if( '1' == pattern.charAt(i) ) {
129 c++;
130 }
131 }
132 return c;
133 }
134 public static long toLong(final String bitPattern) {
135 return Long.valueOf(bitPattern, 2).longValue();
136 }
137 public static int toInteger(final String bitPattern) {
138 final long res = Long.valueOf(bitPattern, 2).longValue();
139 if( res > UNSIGNED_INT_MAX_VALUE ) {
140 throw new NumberFormatException("Exceeds "+toHexString(UNSIGNED_INT_MAX_VALUE)+": "+toHexString(res)+" - source "+bitPattern);
141 }
142 return (int)res;
143 }
144
145 public static String toHexString(final int v) {
146 return "0x"+Integer.toHexString(v);
147 }
148 public static String toHexString(final long v) {
149 return "0x"+Long.toHexString(v);
150 }
151 public static final String strZeroPadding= "0000000000000000000000000000000000000000000000000000000000000000"; // 64
152 public static String toBinaryString(final int v, final int bitCount) {
153 if( 0 == bitCount ) {
154 return "";
155 }
156 final int mask = (int) ( ( 1L << bitCount ) - 1L );
157 final String s0 = Integer.toBinaryString( mask & v );
158 return strZeroPadding.substring(0, bitCount-s0.length())+s0;
159 }
160 public static String toBinaryString(final long v, final int bitCount) {
161 if( 0 == bitCount ) {
162 return "";
163 }
164 final long mask = ( 1L << bitCount ) - 1L;
165 final String s0 = Long.toBinaryString( mask & v );
166 return strZeroPadding.substring(0, bitCount-s0.length())+s0;
167 }
168 public static String toHexBinaryString(final long v, final int bitCount) {
169 final int nibbles = 0 == bitCount ? 2 : ( bitCount + 3 ) / 4;
170 return String.format((Locale)null, "[%0"+nibbles+"X, %s]", v, toBinaryString(v, bitCount));
171 }
172 public static String toHexBinaryString(final int v, final int bitCount) {
173 final int nibbles = 0 == bitCount ? 2 : ( bitCount + 3 ) / 4;
174 return String.format((Locale)null, "[%0"+nibbles+"X, %s]", v, toBinaryString(v, bitCount));
175 }
176 public static String toHexBinaryString(final short v, final int bitCount) {
177 final int nibbles = 0 == bitCount ? 2 : ( bitCount + 3 ) / 4;
178 return String.format((Locale)null, "[%0"+nibbles+"X, %s]", v, toBinaryString(v, bitCount));
179 }
180}
static final String[] testStringsLSB_revByte
static final String[] testStringsMSB
static int getOneBitCount(final String pattern)
static final String[] testStringsMSB_rev
static final String strZeroPadding
static final String testStringLSB_revByte
static final byte[] testBytesMSB_rev
static final String testStringMSB
static final byte[] testBytesLSB_revByte
static String toBinaryString(final int v, final int bitCount)
static String toHexString(final long v)
static String toHexBinaryString(final short v, final int bitCount)
static String toHexBinaryString(final long v, final int bitCount)
static final String testStringMSB_rev
static final long UNSIGNED_INT_MAX_VALUE
static String toHexBinaryString(final int v, final int bitCount)
static String toHexString(final int v)
static final void dumpData(final String prefix, final ByteBuffer data, final int offset, final int len)
static final byte[] testBytesLSB
static final String[] pyramid32bit_one
static int toInteger(final String bitPattern)
static final String[] testStringsLSB
static String toBinaryString(final long v, final int bitCount)
static final String testStringLSB
static final void dumpData(final String prefix, final byte[] data, final int offset, final int len)
static long toLong(final String bitPattern)
static final byte[] testBytesMSB