View Javadoc
1   // ******************************************************************************
2   //
3   // Title:       Force Field X.
4   // Description: Force Field X - Software for Molecular Biophysics.
5   // Copyright:   Copyright (c) Michael J. Schnieders 2001-2024.
6   //
7   // This file is part of Force Field X.
8   //
9   // Force Field X is free software; you can redistribute it and/or modify it
10  // under the terms of the GNU General Public License version 3 as published by
11  // the Free Software Foundation.
12  //
13  // Force Field X is distributed in the hope that it will be useful, but WITHOUT
14  // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15  // FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
16  // details.
17  //
18  // You should have received a copy of the GNU General Public License along with
19  // Force Field X; if not, write to the Free Software Foundation, Inc., 59 Temple
20  // Place, Suite 330, Boston, MA 02111-1307 USA
21  //
22  // Linking this library statically or dynamically with other modules is making a
23  // combined work based on this library. Thus, the terms and conditions of the
24  // GNU General Public License cover the whole combination.
25  //
26  // As a special exception, the copyright holders of this library give you
27  // permission to link this library with independent modules to produce an
28  // executable, regardless of the license terms of these independent modules, and
29  // to copy and distribute the resulting executable under terms of your choice,
30  // provided that you also meet, for each linked independent module, the terms
31  // and conditions of the license of that module. An independent module is a
32  // module which is not derived from or based on this library. If you modify this
33  // library, you may extend this exception to your version of the library, but
34  // you are not obligated to do so. If you do not wish to do so, delete this
35  // exception statement from your version.
36  //
37  // ******************************************************************************
38  package ffx.utilities;
39  
40  /**
41   * ByteSwap class.
42   *
43   * @author Timothy D. Fenn
44   * @since 1.0
45   */
46  public class ByteSwap {
47  
48    /**
49     * Private constructor to prevent instantiation.
50     */
51    private ByteSwap() {
52    }
53  
54    /**
55     * Byte swap a single short value.
56     *
57     * @param value Value to byte swap.
58     * @return Byte swapped representation.
59     */
60    public static short swap(short value) {
61      int b1 = value & 0xff;
62      int b2 = (value >> 8) & 0xff;
63  
64      return (short) (b1 << 8 | b2);
65    }
66  
67    /**
68     * Byte swap a single int value.
69     *
70     * @param value Value to byte swap.
71     * @return Byte swapped representation.
72     */
73    public static int swap(int value) {
74      int b1 = (value) & 0xff;
75      int b2 = (value >> 8) & 0xff;
76      int b3 = (value >> 16) & 0xff;
77      int b4 = (value >> 24) & 0xff;
78  
79      return b1 << 24 | b2 << 16 | b3 << 8 | b4;
80    }
81  
82    /**
83     * Byte swap a single long value.
84     *
85     * @param value Value to byte swap.
86     * @return Byte swapped representation.
87     */
88    public static long swap(long value) {
89      long b1 = (value) & 0xff;
90      long b2 = (value >> 8) & 0xff;
91      long b3 = (value >> 16) & 0xff;
92      long b4 = (value >> 24) & 0xff;
93      long b5 = (value >> 32) & 0xff;
94      long b6 = (value >> 40) & 0xff;
95      long b7 = (value >> 48) & 0xff;
96      long b8 = (value >> 56) & 0xff;
97  
98      return b1 << 56 | b2 << 48 | b3 << 40 | b4 << 32 | b5 << 24 | b6 << 16 | b7 << 8 | b8;
99    }
100 
101   /**
102    * Byte swap a single float value.
103    *
104    * @param value Value to byte swap.
105    * @return Byte swapped representation.
106    */
107   public static float swap(float value) {
108     int intValue = Float.floatToIntBits(value);
109     intValue = swap(intValue);
110 
111     return Float.intBitsToFloat(intValue);
112   }
113 
114   /**
115    * Byte swap a single double value.
116    *
117    * @param value Value to byte swap.
118    * @return Byte swapped representation.
119    */
120   public static double swap(double value) {
121     long longValue = Double.doubleToLongBits(value);
122     longValue = swap(longValue);
123 
124     return Double.longBitsToDouble(longValue);
125   }
126 
127   /**
128    * Byte swap an array of shorts. The result of the swapping is put back into the specified array.
129    *
130    * @param array Array of values to swap
131    */
132   public static void swap(short[] array) {
133     int n = array.length;
134     for (int i = 0; i < n; i++) {
135       array[i] = swap(array[i]);
136     }
137   }
138 
139   /**
140    * Byte swap an array of ints. The result of the swapping is put back into the specified array.
141    *
142    * @param array Array of values to swap
143    */
144   public static void swap(int[] array) {
145     int n = array.length;
146     for (int i = 0; i < n; i++) {
147       array[i] = swap(array[i]);
148     }
149   }
150 
151   /**
152    * Byte swap an array of longs. The result of the swapping is put back into the specified array.
153    *
154    * @param array Array of values to swap
155    */
156   public static void swap(long[] array) {
157     int n = array.length;
158     for (int i = 0; i < n; i++) {
159       array[i] = swap(array[i]);
160     }
161   }
162 
163   /**
164    * Byte swap an array of floats. The result of the swapping is put back into the specified array.
165    *
166    * @param array Array of values to swap
167    */
168   public static void swap(float[] array) {
169     int n = array.length;
170     for (int i = 0; i < n; i++) {
171       array[i] = swap(array[i]);
172     }
173   }
174 
175   /**
176    * Byte swap an array of doubles. The result of the swapping is put back into the specified array.
177    *
178    * @param array Array of values to swap
179    */
180   public static void swap(double[] array) {
181     int n = array.length;
182     for (int i = 0; i < n; i++) {
183       array[i] = swap(array[i]);
184     }
185   }
186 }