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 ?
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 ?
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;
0xff
? 0x80
is enough. I think the right way to do it is int msb = (m & 0x80) >>> 7;
. –
Yama int msb = -(m >> 7);
;) –
Yama 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;
}
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.
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
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.
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
}
}
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);
}
© 2022 - 2025 — McMap. All rights reserved.