0xCA AND 0x5C (8-bit)
- Operation
- A AND B
- A
- 0xCA
- B
- 0x5C
- Width
- 8-bit
- Read the result as
- Unsigned
- Result
- 72
- Hex
- 0x48
- Binary
- 0100 1000
- Bits set to 1
- 2
Checked against: Independent calculation
Bitwise AND, OR, XOR, NOT, NAND, NOR, shifts and rotates on 8- to 64-bit integers, shown in decimal, hex and binary with a bit-by-bit grid.
By AdminDrafted with AI writing toolsUpdated Checked examples: 8
A AND B is 0x48 (0100 1000), which is 72 whether read as signed or unsigned; 2 of the 8 bits are 1.
Negative inputs are stored as 2⁸ = 256 minus their magnitude (two's complement).
Result bit is 1 only where both bits are 1.
Bitwise operators work on each bit position separately: AND gives 1 only where both bits are 1, OR where either is, XOR where they differ, and NOT flips every bit. Shifts move the whole pattern left or right, filling with zeros, or with copies of the sign bit for an arithmetic right shift, and rotates carry the bits that fall off one end back in at the other. Every result is cut to the chosen width of 8, 16, 32 or 64 bits.
Programmers use it to build and test bit masks, flags, permissions and hardware registers. The default, 0xCA AND 0x5C in 8 bits, keeps only the bits the two share: 1100 1010 AND 0101 1100 = 0100 1000, which is 0x48 or 72. XOR of the same pair gives 0x96, which reads as 150 unsigned or −106 signed.
Negative inputs are stored in two's complement, so −1 in 16 bits is 0xFFFF.
In the formula, U is the unsigned value, S the signed value, b_i bit i, and w the width.
Bitwise operators compare two integers one bit position at a time, which is how code tests flags, masks and hardware registers. AND gives 1 only where both bits are 1, OR where either bit is 1, and XOR where they differ. In 8 bits, 0xCA AND 0x5C is 1100 1010 AND 0101 1100 = 0100 1000, or 0x48 = 72.
The same pair through the other operators, still 8 bits wide:
0xCA OR 0x5C = 1101 1110 = 0xDE = 2220xCA XOR 0x5C = 1001 0110 = 0x96 = 150NOT 0xCA = 0011 0101 = 0x35 = 53C, Java, JavaScript and Python write these operators as &, |, ^ and ~. The logical operators && and || (and and or in Python) are a different thing: they test whole values for truth.
A w-bit pattern has two readings. Unsigned, bit i is worth 2^i. Signed, in two's complement, the top bit is worth −2^(w−1) instead, so a negative number −x is stored as 2^w − x.
1111 1101 (0xFD).Eight bits hold −128 to 127 signed or 0 to 255 unsigned. The count of 1 bits, called the population count or popcount, is the same under both readings: 0x96 has four.
Each example moves 0xCA (1100 1010) by 2 places in 8 bits:
<<): zeros enter on the right and bits pushed past the top are lost, giving 0010 1000 = 40. That is 202 × 4 = 808 reduced mod 256.>>> in Java and JavaScript): zeros enter on the left, giving 0011 0010 = 50.>> in Java, JavaScript and Python): copies of the sign bit enter on the left, giving 1111 0010 = 0xF2. As signed bytes, that is −54 ÷ 4 = −13.5 rounded down to −14.An arithmetic shift rounds toward negative infinity, so -3 >> 1 is −2, while -3 / 2 truncates to −1 in C and Java. Rotation counts wrap at the width: rotating 8 bits by 10 equals rotating by 2.
Languages differ at the edges. The C standard draft, section 6.5.7, makes a shift by the operand's width or more undefined and leaves right shifts of negative values implementation-defined.
Java and JavaScript use only the low 5 bits of a 32-bit shift count, so 1 << 32 is 1 in both.
A mask is a number whose 1 bits mark the positions you care about. For bit n of x:
x | (1 << n)x & ~(1 << n)x ^ (1 << n)(x & (1 << n)) != 0x & (x - 1) clears the lowest set bit, so it is 0 only when x is 0 or a power of two. x & -x keeps just the lowest set bit, so 0xCA & -0xCA is 2.
Subnet masks work the same way. 130 AND 192 = 128, which places the address 192.168.1.130/26 in the 192.168.1.128 network.
| Scenario | Decimal result | Hex result | Set bits |
|---|---|---|---|
| Default | 72 | 0x48 | 2 |
| Signed XOR | -106 | 0x96 | 4 |
| Negative arithmetic shift | -2 | 0xFE | 7 |
| Logical negative shift | 126 | 0x7E | 6 |
| 64-bit precision | 9007199254740992 | 0x0020000000000000 | 1 |
Default is 0xCA AND 0x5C in 8 bits, and Signed XOR reads 0x96 as a signed byte. The two shift rows move −3 right by one place.
The last row XORs 2^53 + 1 = 9007199254740993 with 1 in 64 bits. A JavaScript Number cannot hold that value exactly, so the low bit would be lost before the XOR.
x & 1 == 0 parses as x & (1 == 0) and is always 0. Write (x & 1) == 0.~0xCA is −203 in Python. Mask to the width you mean: ~0xCA & 0xFF is 53.0xFFFFFFFF | 0 is −1. Use BigInt for 64-bit work.0x, 0b and 0o. Underscores can group digits, as in 0b0101_1100.Use the number base converter when you only need another base, and the subnet calculator for IP address masks.
Checked against: Independent calculation
Checked against: 0xCA ^ 0x5C = 150
Checked against: ~0 & = 18446744073709551615
Checked against: -128 >> 2 = -32; -32 & 0xFF = 0xE0
XOR (exclusive or) gives 1 where the two bits differ and 0 where they match, so 0xCA XOR 0x5C = 0x96. Two properties make it useful: x XOR x = 0, and applying the same key twice restores the original. That is why XOR toggles flags, computes parity and checksums, and appears in simple ciphers. C, Java, JavaScript and Python all write it as ^.
A logical right shift fills the vacated top bits with 0, while an arithmetic shift copies the sign bit so negative numbers stay negative. In 8 bits, 0x80 shifted right by 2 logically is 0x20 (32), but −128 shifted right by 2 arithmetically is 0xE0 (−32), which is −128 ÷ 4. Java and JavaScript write >>> for logical and >> for arithmetic; in C, >> on a negative signed value is implementation-defined.
A mask is a number whose 1 bits pick the positions you care about. AND with the mask keeps those bits and clears the rest, OR sets them, XOR toggles them, and AND with NOT mask clears them. x AND 0x0F keeps the low 4 bits, and −1 AND 0x0F0F in 16 bits gives 0x0F0F = 3855, because −1 is all ones. To test bit n, check whether x AND (1 << n) is non-zero.
Shifting left by n multiplies an unsigned value by 2^n as long as no 1 bits are pushed past the top: 0x0F << 4 is 0xF0 = 240. Bits that move beyond the width are lost, so in 8 bits 0xFF << 4 is also 0xF0, not 4,080. In C, shifting by the full width of the promoted left operand or more is undefined behavior, and x86 masks the shift count to 5 bits for 32-bit operands and 6 bits for 64-bit ones.
Popcount (population count, or Hamming weight) is the number of 1 bits in a value: 0x48 = 0100 1000 has 2, and NOT 0 in 64 bits has 64. x86 has a single POPCNT instruction for it, C++20 exposes it as std::popcount and Java as Integer.bitCount. It counts set flags and gives the Hamming distance between two values as popcount(a XOR b).
This calculator includes 8 worked examples with independently sourced answers. They run in the test suite, and you can run them here too.