How to clear most significant bit in byte?

Robby Smet

I want to parse a temperature from bytes.

The temperature consists of 2 bytes. The most significant bit of the first byte indicates if the temperature is positive or negative.

This is what I have so far:

public double parseTemperatureBytes(byte[] temperatureBytes) {
    // Must be divided by 10 (1 decimal)
    // First bit indicates sign (bit 1 = negative, bit 0 = positive)
    // Range [0x7FFF] : [-3276.7 … +3276.7]

    byte firstByte = temperatureBytes[0];
    int positiveOrNegative = ParseUtils.getMostSignificantBit(firstByte);
    boolean isPositive = positiveOrNegative == 0;

    String temperatureHex = ParseUtils.bytesToHex(temperatureBytes);
    int temperatureHexToInteger = Integer.parseInt(temperatureHex, 16);
    double temperature = temperatureHexToInteger / (double) 10;

    if (!isPositive) {
        temperature = -temperature;
    }

    return temperature;
}


// ParseUtils

public static int getMostSignificantBit(byte b) {
    return (b & 0xff) >> 7;
}

This works, but I still have to make sure I ignore the most significant bit of the first byte. It's just a flag and not part of the temperature.

Ex: If I pass in 0xFFFF it returns -6553.5 but it should be -3276.7

How can I achieve this?

Ramesh-X

You can check whether the given temperature is minus, using the following code..

public static int getMostSignificantBit(byte b) {
    return b & 0x80;
}

You can use the following code to get your temperature value..

public static double parseTemperatureBytes(byte[] temperatureBytes) {
    int firstByte = temperatureBytes[0] & 0x7F;
    int secondByte = temperatureBytes[1] & 0xFF;
    double temperature = ((firstByte << 8) | secondByte) / 10.0;

    if (getMostSignificantBit(temperatureBytes[0]) > 0) {
        temperature = -temperature;
    }

    return temperature;
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to interpret using Most Significant Bit of single byte - Pcapng time format "if_tsresol"

From Dev

Most Significant Byte Calculation

From Dev

how is the most significant bit radix sort more efficient than the least significant bit radix sort?

From Dev

How do I flip the most significant bit in MIPS?

From Dev

set most significant bit in C

From Dev

Find most significant set bit in a long

From Dev

Comparing the Most Significant Bit of two numbers: ==, <, <=

From Dev

How to write a constant time function to copy the most significant bit to all bits

From Dev

How to change the most significant bit to a 1 after shifting an int to the right using '>>'?

From Dev

Bitwise operators: How do I clear the most significat bit?

From Dev

Get the most significant bit from an 8-bit value

From Dev

Get the most significant bit from an 8-bit value

From Dev

Calculating Most and Least significant byte(MSB, LSB) with Swift

From Dev

The MSB (most significant bit ) in pyserial python sent to arduino uno in damged

From Dev

How to write least significant bit into the buffer?

From Dev

How to Delete Least Significant Bit in Java?

From Dev

Writing a bit reader in JAVA (32-bit little-endian most-to-least-significant bit packing)

From Dev

Fast way of finding most and least significant bit set in a 64-bit integer

From Dev

How to get most significant n bits from int in java

From Dev

Create a mask that marks the most significant set bit, using only bitwise operators

From Dev

Hex of most significant nibble

From Dev

How can I get the value of the least significant bit in a number?

From Dev

How to Replace Least Significant Bit In Buffered Image in Scala

From Dev

Xor starting with the significant bit

From Dev

Clearing least significant bit

From Dev

Least significant bit mips

From Dev

Get most significant digit in python

From Dev

Getting least significant bit in JavaScript

From Dev

Extraction of the least significant bit of a pixel

Related Related

  1. 1

    How to interpret using Most Significant Bit of single byte - Pcapng time format "if_tsresol"

  2. 2

    Most Significant Byte Calculation

  3. 3

    how is the most significant bit radix sort more efficient than the least significant bit radix sort?

  4. 4

    How do I flip the most significant bit in MIPS?

  5. 5

    set most significant bit in C

  6. 6

    Find most significant set bit in a long

  7. 7

    Comparing the Most Significant Bit of two numbers: ==, <, <=

  8. 8

    How to write a constant time function to copy the most significant bit to all bits

  9. 9

    How to change the most significant bit to a 1 after shifting an int to the right using '>>'?

  10. 10

    Bitwise operators: How do I clear the most significat bit?

  11. 11

    Get the most significant bit from an 8-bit value

  12. 12

    Get the most significant bit from an 8-bit value

  13. 13

    Calculating Most and Least significant byte(MSB, LSB) with Swift

  14. 14

    The MSB (most significant bit ) in pyserial python sent to arduino uno in damged

  15. 15

    How to write least significant bit into the buffer?

  16. 16

    How to Delete Least Significant Bit in Java?

  17. 17

    Writing a bit reader in JAVA (32-bit little-endian most-to-least-significant bit packing)

  18. 18

    Fast way of finding most and least significant bit set in a 64-bit integer

  19. 19

    How to get most significant n bits from int in java

  20. 20

    Create a mask that marks the most significant set bit, using only bitwise operators

  21. 21

    Hex of most significant nibble

  22. 22

    How can I get the value of the least significant bit in a number?

  23. 23

    How to Replace Least Significant Bit In Buffered Image in Scala

  24. 24

    Xor starting with the significant bit

  25. 25

    Clearing least significant bit

  26. 26

    Least significant bit mips

  27. 27

    Get most significant digit in python

  28. 28

    Getting least significant bit in JavaScript

  29. 29

    Extraction of the least significant bit of a pixel

HotTag

Archive