I’m curious to know why, when I compare a byte array with a value…
boolean match = ((data[0] & 0xFF) == 0xFE);
…returns true, while…
boolean match = (data[0] == 0xFE);
…does not? data is a byte array with
data[0] = (byte) 0xFE;
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
compares integers as 0xFF is an integer, this expression will scale up your byte
data[0]to an int and compare what’s inside the parenthesis to a second int0xFE(254). As you saydata[0]is(byte)0xFE, it will first be scaled to the integer0xFEand compared to the integer0xFE, so this works.compares a byte to the int
0xFE:254is a byte (so it’s signed) and its value is
-2.-2is not equal to254, so that’s why you must compare data[0] as a byte or as scale it up to an integer before comparing it the integer0xFE.A simpler comparison could be