Toggle bits and see denary, signed, binary and hex update together, with shifts and two’s complement.
Convert between binary, denary and hex, and practise two’s complement.
This tool needs JavaScript.
Denary uses ten digits, binary uses two (0 and 1), and hexadecimal uses sixteen (0 to 9, then A to F for 10 to 15). In an 8-bit binary number the place values, from the left, are 128, 64, 32, 16, 8, 4, 2 and 1.
Two’s complement stores negative integers by giving the top bit a negative weight: −128 in 8 bits, so the range is −128 to 127. To negate a number, invert every bit and then add 1. The AQA 8525 and OCR J277 GCSE specifications don’t mention two’s complement, and SQA National 5 covers positive integers only. It is part of SQA Higher.
Overflow means a result does not fit in the bits available. For unsigned addition, it is a carry out of the top bit. In two’s complement, it is two positives giving a negative result, or two negatives giving a positive one; a carry out alone does not show it.
A logical shift moves every bit and fills the gap with 0. Each place left multiplies an unsigned number by 2, if no 1 is lost off the end. Each place right divides it by 2 and discards the remainder. AQA GCSE uses only logical shifts; OCR GCSE asks for both left and right. An arithmetic right shift copies the top bit instead, which keeps the sign.
37 is 32 + 4 + 1. Invert its bits, then add 1.
Check it with the signed weights: −128 + 64 + 16 + 8 + 2 + 1 = −37. Read as unsigned, the same bits are 219, which is DB in hex (1101 is D and 1011 is B).
Open 37 in the bit sandbox, then press Negate
The bits alone do not say. The question tells you how to read them. In two’s complement a leading 1 means negative; in an unsigned number it is worth +128 in a byte.
One hex digit stands for exactly four bits, so a byte takes two digits. That is shorter than binary, easier for people to read and copy, and quick to convert back.
01100100 + 00110010 gives 10010110, which reads as −106. The true total, 150, is above 127, so the addition has overflowed.