Download presentation
Presentation is loading. Please wait.
1
Bits and Bytes Hex Digit Bit Pattern 0000 1 0001 2 0010 3 0011 4 0100
0000 1 0001 2 0010 3 0011 4 0100 5 0101 6 0110 7 0111 8 1000 9 1001 a 1010 b 1011 c 1100 d 1101 e 1110 f 1111 One bit stores either 0 or 1. One byte is a sequence of 8 bits. One Java int is a sequence of 4 bytes or 32 bits. Each half (nibble) of a byte can be represented by a single hexadecimal digit. To express Java data (int) in hexadecimal use… 0xHHHHHHHH where "H" is one hex digit Examples 0x00ffffff 0xff00ffff 0xab
2
Bitwise Operations Hex Digit Bit Pattern 0000 1 0001 2 0010 3 0011 4
0000 1 0001 2 0010 3 0011 4 0100 5 0101 6 0110 7 0111 8 1000 9 1001 a 1010 b 1011 c 1100 d 1101 e 1110 f 1111 & means bitwise AND Each bit is treated as follows: 0 & 0 0 0 & 1 0 1 & 0 0 1 & 1 1 | means bitwise OR Each bit is treated as follows: 0 | 0 0 0 | 1 1 1 | 0 1 1 | 1 1 Example int bits = 0xabcdefab; bits = bits & 0xff00ffff; bits = 0xabcdefab; bits = bots | 0xff00ffff;
3
More Bitwise Operations
Hex Digit Bit Pattern 0000 1 0001 2 0010 3 0011 4 0100 5 0101 6 0110 7 0111 8 1000 9 1001 a 1010 b 1011 c 1100 d 1101 e 1110 f 1111 <<< n means shift left by n (n is an integer) >>> n means shift right by n (n is an integer) Example int bits = 0x00abcdefab; bits = bits >>> 8; bits = bits <<< 8; bits = 0xff00ffff; bits = bits >>> 16; Two more things… 1) A cast to byte returns the rightmost byte of int, long or short data. 2) Treating a byte as an int, short or long sign extends. Example int bits = 0x00abcdefab; byte oneByte = (byte)bits; bits = oneByte;
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.