[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-08-07 UTC."],[[["\u003cp\u003eGoogleSQL for BigQuery offers several bit functions, including \u003ccode\u003eBIT_AND\u003c/code\u003e, \u003ccode\u003eBIT_COUNT\u003c/code\u003e, \u003ccode\u003eBIT_OR\u003c/code\u003e, and \u003ccode\u003eBIT_XOR\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003e\u003ccode\u003eBIT_COUNT\u003c/code\u003e calculates the total number of bits set to 1 within a given integer or \u003ccode\u003eBYTES\u003c/code\u003e input expression.\u003c/p\u003e\n"],["\u003cp\u003eThe input expression used with \u003ccode\u003eBIT_COUNT\u003c/code\u003e can include integers and \u003ccode\u003eBYTES\u003c/code\u003e while the result is an \u003ccode\u003eINT64\u003c/code\u003e representing the total bits.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003eBIT_COUNT\u003c/code\u003e function also works on signed integers using two's complement form, calculating the set bits appropriately.\u003c/p\u003e\n"]]],[],null,["# Bit functions\n\nGoogleSQL for BigQuery supports the following bit functions.\n\nFunction list\n-------------\n\n`BIT_COUNT`\n-----------\n\n BIT_COUNT(expression)\n\n**Description**\n\nThe input, `expression`, must be an\ninteger or `BYTES`.\n\nReturns the number of bits that are set in the input `expression`.\nFor signed integers, this is the number of bits in two's complement form.\n\n**Return Data Type**\n\n`INT64`\n\n**Example** \n\n SELECT a, BIT_COUNT(a) AS a_bits, FORMAT(\"%T\", b) as b, BIT_COUNT(b) AS b_bits\n FROM UNNEST([\n STRUCT(0 AS a, b'' AS b), (0, b'\\x00'), (5, b'\\x05'), (8, b'\\x00\\x08'),\n (0xFFFF, b'\\xFF\\xFF'), (-2, b'\\xFF\\xFF\\xFF\\xFF\\xFF\\xFF\\xFF\\xFE'),\n (-1, b'\\xFF\\xFF\\xFF\\xFF\\xFF\\xFF\\xFF\\xFF'),\n (NULL, b'\\xFF\\xFF\\xFF\\xFF\\xFF\\xFF\\xFF\\xFF\\xFF\\xFF')\n ]) AS x;\n\n /*-------+--------+---------------------------------------------+--------*\n | a | a_bits | b | b_bits |\n +-------+--------+---------------------------------------------+--------+\n | 0 | 0 | b\"\" | 0 |\n | 0 | 0 | b\"\\x00\" | 0 |\n | 5 | 2 | b\"\\x05\" | 2 |\n | 8 | 1 | b\"\\x00\\x08\" | 1 |\n | 65535 | 16 | b\"\\xff\\xff\" | 16 |\n | -2 | 63 | b\"\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xfe\" | 63 |\n | -1 | 64 | b\"\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\" | 64 |\n | NULL | NULL | b\"\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\" | 80 |\n *-------+--------+---------------------------------------------+--------*/"]]