Unix file permissions encode read, write, and execute rights for the owner, the owner’s group, and everyone else. Each permission is a bit: read is 4, write is 2, and execute is 1, so an octal mode such as 764 represents three groups of bits (owner, group, others). The account explains an R permission check that uses bitwise AND rather than comparing mode numbers directly. Applying `& "400"` keeps only the owner-read bit: the result is either 400, if that permission is set, or 000 if it is not. Comparing the result with 400 therefore identifies files their owner cannot read.
Masking matters because numeric comparison considers unrelated permission bits. A mode such as 1377 includes a sticky bit, making its numeric value greater than 400, even though the owner lacks read permission. A direct comparison would incorrectly treat it as sufficient; the mask isolates the permission the check actually needs. The same method checks directories against 700, requiring the owner to have read, write, and execute rights: if any of those bits is absent, the masked result falls below 700. The general pattern, `(mode & required_bits) < required_bits`, detects whether any required permission is missing. The discussion also demonstrates changing a file’s mode in R and explains why this logic appears in package checks, including checks for scripts that need 500.
Summary generated by AI from the linked article. hn.today is not affiliated with Hacker News or Y Combinator.