Subject: Getting a bit from a byte 60. ***** Q: How do I obtain a bit or bits from a byte, a word or a longint? A: For bit operations think of the variable as a binary number instead of a decimal. Consider for example var x : word; x := 219; In binary presentation it is The word 0000 0000 1101 1011 Position in the word FEDC BA98 7654 3210 Say you need the value of bit 6 (the seventh bit) in the word. You can "and" the following words 0000 0000 1101 1011 (219) 0000 0000 0100 0000 ( 64) In decimal TP notation this amounts to var b : word; b := x and 64; The value of b is now 0000 0000 0100 0000 ( 64) To get the bit value (0 or 1) you need to shift the result right by six steps, that this the expression becomes the often seen but cryptic b := (x and 64) shr 6; which means that the value of b is finally 1 in this example. Ok, but what then if you need the combined value of bits six and seven. The answer is evident if you consider the binary presentation 0000 0000 1101 1011 (219) 0000 0000 1100 0000 (192) hence b := (x and 192) shr 6; which will give 3 as it should. So far, so good. What if you need to turn on bit nine in a word without interfering with the other bits. The binary presentation, again, is the key. You'll have to "or" the following 0000 0000 1101 1011 (219) 0000 0010 0000 0000 (512) that is x := x or 512; This results to 0000 0010 1101 1011 (731) What if you wish to turn off, say bit 6, in 0000 0000 1101 1011 (219) 1111 1111 1011 1111 (65471) This is achieved by x := 219; x := x and 65471; This results to 0000 0000 1001 1011 (155) Consider the following application as an example. The number of a PC's floppy disk drives (minus one) is stored in bits 6 and 7 in a word returned by interrupt $11. This is the code to find out how many disk drives a PC has. uses Dos; function NrOfFDiskDrives : byte; var regs : registers; begin Intr ($11, regs); NrOfFDiskDrives := ((regs.ax and 192) shr 6) + 1; end; A tip from Duncan Murdoch. You might wish to predefine the following constants for easier handling const bit0 = 1; bit1 = 2; bit2 = 4; : bit15 = 32768; : bit31 = 2147483648; Or to put it slightly differently as Dr John Stockton jrs@dclf.npl.co.uk suggests const bit00=$00000001; bit01=$00000002; bit02=$00000004; bit03=$00000008; bit04=$00000010; bit05=$00000020; bit06=$00000040; bit07=$00000080; : bit28=$10000000; bit29=$20000000; bit30=$40000000; bit31=$80000000; Finally, you also might want to look at the item "Getting a nybble from a byte". --------------------------------------------------------------------