How to find most significant bit (MSB)
Asked Answered
O

7

5

I want to know which value the first bit of a byte has.

For example:

I have byte m = (byte) 0x8C;

How could I know if the first bit is an 1 or a 0 ?

Can anyone help me out ?

Oestrone answered 28/9, 2011 at 17:16 Comment(2)
What does "first" mean, specifically?Ennoble
If you want the position of the first 1, from the left: use Integer.numberOfLeadingZeroesStraightway
P
24

It depends what you mean by "first bit". If you mean "most significant bit" you can use:

// 0 or 1
int msb = (m & 0xff) >> 7;

Or if you don't mind the values being 0x80 or 0, just use:

// 0 or 0x80
int msb = m & 0x80;

Or in fact, as a boolean:

// Uses the fact that byte is signed using 2s complement
// True or false
boolean msb = m < 0;

If you mean the least significant bit, you can just use:

// 0 or 1
int lsb = m & 1;
Plea answered 28/9, 2011 at 17:19 Comment(5)
It was the most significant bit. Thank you for your helpMethyl
@Jon I don't understand the first one why it's 0xff? 0x80 is enough. I think the right way to do it is int msb = (m & 0x80) >>> 7;.Yama
@Eng.Fouad: Yes, that would be fine. I just habitually convert byte to int by masking with 0xff :)Plea
@Jon but still doesn't make sense for me. However, more simple way to do it is int msb = -(m >> 7); ;)Yama
@Eng.Fouad: Well it's still going to have the same result, isn't it? (Masking with 0xff I mean.)Plea
D
3

Assuming you mean leftmost bit, bitwise and it with 0x80 and check if it is zero nor not:

public boolean isFirstBitSet(byte b) {
    System.out.println((b & (byte)0x80));
    return (b & (byte)0x80) < 0;
}

If you mean lowest order bit you will need to and with 0x01 and check a different condition:

public boolean isFirstBitSet(byte b) {
    System.out.println((b & (byte)0x01));
    return (b & (byte)0x80) > 0;
}
Depreciate answered 28/9, 2011 at 17:18 Comment(0)
A
3

If the first bit is the lowest bit (ie bit 0), then

if((m & 1) >0) ...

should do it.

In general,

if ((m & (1<<N)) > 0) ...

will give you whether or not bit N is set. If, however, you meant the highest bit (bit 7), then use N=7.

Augean answered 28/9, 2011 at 17:19 Comment(0)
P
1

Use the bitwise and operator.

public class BitExample {
    public static void main(String args[]) throws Exception {
        byte m = (byte)0x8C;
        System.out.println("The first bit is " + (m & (byte)0x01));
        m = (byte)0xFF;
        System.out.println("The first bit is " + (m & (byte)0x01));
    }
}

// output is...
The first bit is 0
The first bit is 1
Putrid answered 28/9, 2011 at 17:20 Comment(0)
C
1

Its a bit of a hack but you can use

if(x >> -1 != 0) // top bit set.

This works for byte, short, int, long data types.

However for most types the simplest approach is to compare with 0

if (x < 0) // top bit set.

This works for byte, short, int, long, float, or double

(Ignoring negative zero and negative NaN, most people do ;)

For char type you need to know the number of bits. ;)

if (ch >>> 15 != 0) // top bit set.
Calpe answered 28/9, 2011 at 18:11 Comment(0)
C
0

This code gives you msb(most significant bit)/biggest number which is a power of 2 and less than any given number.

class msb{
void main(){
        int n=155;//let 110001010
        int l=(Integer.toBinaryString(n)).length();//9
        String w="0"+Integer.toBinaryString(i).substring(1);//010001010
        i=Integer.parseInt(w,2);
        System.out.println(n^i);//110001010^010001010=100000000
}
}
Cyr answered 3/6, 2022 at 9:10 Comment(1)
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.Reface
S
0

In general, the Most significant bit (MSB) is the bit in a multiple-bit binary number with the largest value.

To find the MSB, you can use

int msb = Integer.highestOneBit(n);

n = 11 (1011) will give 8 (1000)

To find the position of the MSB, you can use

int mostSigBitPosition(int n) {
    return 32 - Integer.numberOfLeadingZeros(n);
}

n = 11 (1011) will give you 4 (if we index bits from 1, the MSB is at position 4)

Where Integer.highestOneBit(n) and Integer.numberOfLeadingZeros(n) are java.lang.Integer class's methods

int highestOneBit(int i) {
    return i & (0x80000000 >>> numberOfLeadingZeros(i));
}
int numberOfLeadingZeros(int i) {
  if (i <= 0)
    return i == 0 ? 32 : 0;
  int n = 31;a
  if (i >= 1 << 16) { n -= 16; i >>>= 16; }
  if (i >= 1 <<  8) { n -=  8; i >>>=  8; }
  if (i >= 1 <<  4) { n -=  4; i >>>=  4; }
  if (i >= 1 <<  2) { n -=  2; i >>>=  2; }
  return n - (i >>> 1);
}

Salman answered 19/2, 2024 at 6:31 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.