{"record":{"id":"370e3ee651445af0","repo":"gchq/CyberChef","slug":"error-bit-argument-must-be-between-0-and-7-370e3e","errorCode":null,"errorMessage":"Error: Bit argument must be between 0 and 7","messagePattern":"Error: Bit argument must be between 0 and 7","errorType":"validation","errorClass":"OperationError","httpStatus":null,"severity":"error","filePath":"src/core/operations/ViewBitPlane.mjs","lineNumber":69,"sourceCode":"    async run(input, args) {\n        if (!isImage(input))\n            throw new OperationError(\"Please enter a valid image file.\");\n\n        const [colour, bit] = args;\n        let parsedImage;\n        try {\n            parsedImage = await Jimp.read(input);\n        } catch (err) {\n            throw new OperationError(`Error loading image. (${err})`);\n        }\n\n        const width = parsedImage.bitmap.width,\n            height = parsedImage.bitmap.height,\n            colourIndex = COLOUR_OPTIONS.indexOf(colour),\n            bitIndex = 7 - bit;\n\n        if (bit < 0 || bit > 7) {\n            throw new OperationError(\n                \"Error: Bit argument must be between 0 and 7\",\n            );\n        }\n\n        let pixel, bin, newPixelValue;\n\n        parsedImage.scan(0, 0, width, height, function (x, y, idx) {\n            pixel = this.bitmap.data[idx + colourIndex];\n            bin = Utils.bin(pixel);\n            newPixelValue = 255;\n\n            if (bin.charAt(bitIndex) === \"1\") newPixelValue = 0;\n\n            for (let i = 0; i < 3; i++) {\n                this.bitmap.data[idx + i] = newPixelValue;\n            }\n            this.bitmap.data[idx + 3] = 255;\n        });","sourceCodeStart":51,"sourceCodeEnd":87,"githubUrl":"https://github.com/gchq/CyberChef/blob/4290ea753912378913b1f3f54e0fc5720afeda5d/src/core/operations/ViewBitPlane.mjs#L51-L87","documentation":"Thrown by 'View Bit Plane' when the bit-plane argument is outside 0-7. The bit argument selects one of the 8 bits of a colour channel; values outside that range have no valid bit index (the code computes bitIndex = 7 - bit).","triggerScenarios":"Setting the bit argument below 0 or above 7 via the Node API or a hand-edited recipe. In the UI the control is a number spinner; a manually crafted recipe can carry an out-of-range value.","commonSituations":"Programmatically building a recipe with an off-by-one or negative bit index, or mis-translating a 'bit significance' convention (LSB=0 vs MSB=0).","solutions":["Set the bit argument to an integer in the range 0-7 inclusive.","If your convention numbers bits from the MSB, map it to 7 minus that index.","Validate recipe JSON before running when bit is supplied externally."],"exampleFix":"// before\nviewBitPlane(input, [\"Red\", 8]);\n// after\nviewBitPlane(input, [\"Red\", 7]);","handlingStrategy":"validation","validationCode":"if (!Number.isInteger(bit) || bit < 0 || bit > 7) {\n  throw new Error(`Bit argument must be an integer 0-7, got ${bit}`);\n}","typeGuard":"function isValidBitPlane(b) { return Number.isInteger(b) && b >= 0 && b <= 7; }","tryCatchPattern":null,"preventionTips":["Clamp bit index to 0-7 when building recipes programmatically.","Account for MSB-vs-LSB numbering conventions."],"tags":["image","validation","argument-error","range"],"backgroundTag":null,"analyzedSha":"4290ea753912378913b1f3f54e0fc5720afeda5d","analyzedAt":"2026-08-13T06:05:50.210Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}