{"id":22513562,"url":"https://github.com/emahtab/reverse-bits","last_synced_at":"2026-02-28T15:02:02.608Z","repository":{"id":79525742,"uuid":"275278382","full_name":"eMahtab/reverse-bits","owner":"eMahtab","description":null,"archived":false,"fork":false,"pushed_at":"2020-06-27T02:06:34.000Z","size":22,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-08-25T12:43:47.473Z","etag":null,"topics":["bit-manipulation","leetcode","problem-solving","reverse-bits"],"latest_commit_sha":null,"homepage":"","language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/eMahtab.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2020-06-27T01:27:24.000Z","updated_at":"2024-04-05T22:38:58.000Z","dependencies_parsed_at":"2023-05-10T17:30:20.040Z","dependency_job_id":null,"html_url":"https://github.com/eMahtab/reverse-bits","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/eMahtab/reverse-bits","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Freverse-bits","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Freverse-bits/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Freverse-bits/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Freverse-bits/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/eMahtab","download_url":"https://codeload.github.com/eMahtab/reverse-bits/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Freverse-bits/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29938976,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-28T13:49:17.081Z","status":"ssl_error","status_checked_at":"2026-02-28T13:48:50.396Z","response_time":90,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["bit-manipulation","leetcode","problem-solving","reverse-bits"],"created_at":"2024-12-07T03:13:16.997Z","updated_at":"2026-02-28T15:02:02.580Z","avatar_url":"https://github.com/eMahtab.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"# Reverse Bits\n## https://leetcode.com/problems/reverse-bits\n\nReverse bits of a given 32 bits unsigned integer.\n```\nExample 1:\n\nInput: 00000010100101000001111010011100\nOutput: 00111001011110000010100101000000\nExplanation: The input binary string 00000010100101000001111010011100 represents the unsigned integer 43261596, \nso return 964176192 which its binary representation is 00111001011110000010100101000000.\n\nExample 2:\n\nInput: 11111111111111111111111111111101\nOutput: 10111111111111111111111111111111\nExplanation: The input binary string 11111111111111111111111111111101 represents the unsigned integer 4294967293, \nso return 3221225471 which its binary representation is 10111111111111111111111111111111.\n``` \n\n**Note:**\n\n1. Note that in some languages such as Java, there is no unsigned integer type. In this case, both input and output will be given as signed integer type and should not affect your implementation, as the internal binary representation of the integer is the same whether it is signed or unsigned.\n\n2. In Java, the compiler represents the signed integers using 2's complement notation. Therefore, in Example 2 above the input represents the signed integer -3 and the output represents the signed integer -1073741825.\n \n**Follow up:**\n\nIf this function is called many times, how would you optimize it?\n\n## Approach :\nIdea is to retrieve the last bit from the unsigned number one by one and add it to the result.\n\n![Reverse Bits](reverse-bits.png?raw=true \"Reverse Bits\")\n\nQuick recap about `AND` operator\n```\n1 \u0026 1 = 1\n0 \u0026 1 = 0\n```\nSo we will do `AND` operation (\u0026 1) to retrieve the bit\n\nQuick recap about `OR` operator\n```\n0 | 0 = 0\n1 | 0 = 1\n```\nSo we will do `OR` operation (| bit) to insert the bit into result.\n\nIterate over the 32 bit unsigned number and do the following operations :\n\n1. First do the `\u0026 1` operation with input n, this will give us the last bit\n2. Right shift the input n by 1 (n \u003e\u003e 1), for the next iteration\n3. Next Left shift the result by 1 (result \u003c\u003c 1) to make the space, so now the last bit of result will be 0\n4. Do the `OR` operation with the bit in Step 1 (result | bit)\n\n\n## Implementation :\n```java\npublic class Solution {\n    // you need treat n as an unsigned value\n    public int reverseBits(int n) {\n        int result = 0;\n        for (int i = 0; i \u003c 32; i++) {\n            int bit = n \u0026 1;\n            n = n \u003e\u003e 1;\n            result = result \u003c\u003c 1;\n            result = result | bit;\n        }\n        return result;\n    }\n}\n```\n\n# References :\n1. https://www.youtube.com/watch?v=KE5Axm7uzok\n2. https://developersinspired.com/2020/03/02/how-to-reverse-bits-bit-manipulation\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Femahtab%2Freverse-bits","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Femahtab%2Freverse-bits","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Femahtab%2Freverse-bits/lists"}