{"record":{"id":"bf9f710b52c3a8c2","repo":"gchq/CyberChef","slug":"error-bit-argument-must-be-between-0-and-7","errorCode":null,"errorMessage":"Error: Bit argument must be between 0 and 7","messagePattern":"Error: Bit argument must be between 0 and 7","errorType":"exception","errorClass":"OperationError","httpStatus":null,"severity":"error","filePath":"src/core/operations/ExtractLSB.mjs","lineNumber":86,"sourceCode":"     * @param {Object[]} args\n     * @returns {byteArray}\n     */\n    async run(input, args) {\n        if (!isImage(input))\n            throw new OperationError(\"Please enter a valid image file.\");\n\n        const bit = 7 - args.pop(),\n            pixelOrder = args.pop(),\n            colours = args\n                .filter((option) => option !== \"\")\n                .map((option) => COLOUR_OPTIONS.indexOf(option)),\n            parsedImage = await Jimp.read(input),\n            width = parsedImage.bitmap.width,\n            height = parsedImage.bitmap.height,\n            rgba = parsedImage.bitmap.data;\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 i,\n            combinedBinary = \"\";\n\n        if (pixelOrder === \"Row\") {\n            for (i = 0; i < rgba.length; i += 4) {\n                for (const colour of colours) {\n                    combinedBinary += Utils.bin(rgba[i + colour])[bit];\n                }\n            }\n        } else {\n            let rowWidth;\n            const pixelWidth = width * 4;\n            for (let col = 0; col < width; col++) {\n                for (let row = 0; row < height; row++) {","sourceCodeStart":68,"sourceCodeEnd":104,"githubUrl":"https://github.com/gchq/CyberChef/blob/4290ea753912378913b1f3f54e0fc5720afeda5d/src/core/operations/ExtractLSB.mjs#L68-L104","documentation":"Thrown by the Extract LSB operation when the 'Bit' argument, after transformation (bit = 7 - args.pop()), falls outside the valid range [0, 7]. Each pixel channel value is 8 bits, so only bit positions 0–7 are valid for LSB extraction. The argument is popped from the end of the args array.","triggerScenarios":"async run(input, args) where the computed bit value (7 - args[5], the 'Bit' number argument) is less than 0 or greater than 7. This means the raw 'Bit' argument was > 7 or < 0 before the 7-N transformation.","commonSituations":"User enters a Bit value of 8 or higher, or a negative number, in the operation's 'Bit' argument field. Also if a recipe programmatically supplies an out-of-range bit index.","solutions":["Set the 'Bit' argument to an integer between 0 and 7 inclusive.","If using 0, note bit = 7 - 0 = 7 (most significant); if using 7, bit = 0 (least significant).","Validate the argument before running the operation."],"exampleFix":"// before: args 'Bit' = 10 -> bit = 7 - 10 = -3 -> error\n\n// after: args 'Bit' = 0 -> bit = 7 (valid range)","handlingStrategy":"validation","validationCode":"// Validate Bit argument before calling ExtractLSB\nconst bitArg = args[5];\nif (typeof bitArg !== 'number' || bitArg < 0 || bitArg > 7) {\n  throw new Error('Bit argument must be an integer between 0 and 7');\n}","typeGuard":"function isValidBitArg(bit) {\n  return Number.isInteger(bit) && bit >= 0 && bit <= 7;\n}","tryCatchPattern":null,"preventionTips":["Restrict the Bit argument to integers 0–7.","Validate argument bounds before recipe execution.","Remember bit is internally transformed (7 - arg), so raw values 0–7 map inversely."],"tags":["image","steganography","argument-validation","lsb","range-error"],"backgroundTag":null,"analyzedSha":"4290ea753912378913b1f3f54e0fc5720afeda5d","analyzedAt":"2026-08-13T06:05:50.210Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}