blob: 4e7a6f566e39b59835193bcdeec2431f6292e9dc [file] [log] [blame]
Jian Li47671902016-08-11 01:18:18 +09001/*
2 * Copyright 2016-present Open Networking Laboratory
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.onlab.util;
17
18/**
19 * Provide a set of byte operations.
20 */
21public final class ByteOperator {
22
23 /**
24 * Private constructor which prevents from external instantiation.
25 */
26 private ByteOperator() {
27
28 }
29
30 /**
31 * Obtains a specific bit value from a byte with given index number.
32 *
33 * @param value byte value
34 * @param index index number
35 * @return a specific bit value from a byte
36 */
37 public static boolean getBit(byte value, int index) {
38 // the length of byte should always be positive whiles less than 8
39 if (index > 7 || index < 0) {
40 return false;
41 }
42
Jian Li0a439d22017-02-06 01:05:41 +090043 return (value & (0x1 << index)) != 0;
Jian Li47671902016-08-11 01:18:18 +090044 }
45
46 /**
47 * Converts boolean value into bit.
48 *
49 * @param value boolean value
50 * @param bit bit value
51 * @return converted bit value
52 */
53 public static byte toBit(boolean value, int bit) {
54 return (byte) (value ? bit : 0x00);
55 }
Jian Li47671902016-08-11 01:18:18 +090056}