byte value = 0xfe; // corresponds to lớn -2 (signed) & 254 (unsigned)int result = value & 0xff;The result is 254 when printed, but I have sầu no idea how this code works. If the và operator is simply bitwise, then why does it not result in a byte & instead an integer?



It sets result lớn the (unsigned) value resulting from putting the 8 bits of value in the lowest 8 bits of result.

Bạn đang xem: Schedule task (0xff) error while running batch file via schedule task

The reason something like this is necessary is that byte is a signed type in Java. If you just wrote:

int result = value;then result would over up with the value ff ff ff fe instead of 00 00 00 fe. A further subtlety is that the & is defined khổng lồ operate only on int values1, so what happens is:

value is promoted lớn an int (ff ff ff fe).0xff is an int literal (00 00 00 ff).The và is applied khổng lồ yield the desired value for result.

(The point is that conversion to lớn int happens before the và operator is applied.)

1Well, not quite. The & operator works on long values as well, if either oper& is a long. But not on byte. See the Java Language Specification, sections 15.22.1 & 5.6.2.

Improve this answer
edited Jul 7 "16 at 2:08
answered Jul 8 "12 at 1:50

Ted HoppTed Hopp
223k4848 gold badges373373 silver badges491491 bronze badges
| Show 1 more phản hồi

The hex literal 0xFF is an equal int(255). Java represents int as 32 bits. It look lượt thích this in binary:

00000000 00000000 00000000 11111111When you vì chưng a bit wise AND with this value(255) on any number, it is going to lớn mask(make ZEROs) all but the lowest 8 bits of the number (will be as-is).

... 01100100 00000101 và ...00000000 11111111 = 00000000 00000101và is something lượt thích % but not really.

Xem thêm: Cách Active Office 2013 Bằng Key, Active Office 2013

And why 0xff? this in ((power of 2) - 1).All ((power of 2) - 1) (e.g 7, 255...) will behave sầu something like % operator.

ThenIn binary, 0 is, all zeros, and 255 looks like this:

00000000 00000000 00000000 11111111And -1 looks like this

11111111 11111111 11111111 11111111When you bởi vì a bitwise AND of 0xFF and any value from 0 lớn 255, the result is the exact same as the value. And if any value higher than 255 still the result will be within 0-255.

However, if you do:

-1 & 0xFFyou get

00000000 00000000 00000000 11111111, which does NOT equal the original value of -1 (11111111 is 255 in decimal).

Few more bit manipulation: (Not related lớn the question)

X >> 1 = X/2X Cheông xã any particular bit is set(1) or not (0) then

int thirdBitTobeChecked = 1 0) //Third Bit is set to 1 Set(1) a particular bit

int thirdBitTobeSet = 1 ReSet(0) a particular bit

int thirdBitTobeReSet = ~(1 XOR

Just note that if you perkhung XOR operation twice, will results the same value.

byte toBeEncrypted = 0010 0110byte salt = 0100 1011byte encryptedVal = toBeEncrypted ^ salternative text == 0110 1101byte decryptedVal = encryptedVal ^ salt == 0010 0110 == toBeEncrypted :)One more xúc tích with XOR is

if A (XOR) B == C (salt)then C (XOR) B == A C (XOR) A == BThe above sầu is useful to swap two variables without temp lượt thích below