Swap Adjacent Bits in a 32-Bit Word
Updated
Problem statement
Given a nonnegative integer value representing an unsigned 32-bit word, swap bits in every adjacent pair: positions 0 and 1, positions 2 and 3, and so on through positions 30 and 31. Position 0 is the least significant bit.
Return the resulting 32-bit word as a nonnegative integer. Leading zero bits are part of the word. For example, binary 1001 becomes 0110, so 9 becomes 6.
Function
swapAdjacentBits(value: long) → longExamples
Example 1
value = 9return = 6Low-order binary 1001 becomes 0110, which is 6.
Example 2
value = 1return = 2The only set bit moves from position 0 to position 1.
Example 3
value = 2147483649return = 1073741826The highest and lowest set bits move to positions 30 and 1.
Constraints
0 <= value <= 4294967295.- Interpret
valueas exactly 32 unsigned bits. The input and output use a signed 64-bit integer type so every 32-bit result remains nonnegative.
More NVIDIA Onsite Interview questions
- Check Endianness Of A Byte ArrayONSITE INTERVIEW · Seen Sep 2026
- Implement MallocONSITE INTERVIEW · Seen Sep 2026
- Implement PrintfONSITE INTERVIEW · Seen Sep 2026
- Merge Two Arrays Into Sorted OrderONSITE INTERVIEW · Seen Sep 2026
- Middle Node Of A Linked ListONSITE INTERVIEW · Seen Sep 2026
- Ping Reply TrackingONSITE INTERVIEW · Seen Sep 2026
- Reverse A StringONSITE INTERVIEW · Seen Sep 2026
- Maximum Fish in a GridONSITE INTERVIEW · Seen Sep 2024
- Minimum in a Rotated Sorted ArrayONSITE INTERVIEW · Seen Sep 2024
- First Register Overflow TickONSITE INTERVIEW · Seen May 2024
Similar questions at other companies
- Find the Missing Number from 1 to nHyperVerge · ONSITE INTERVIEW
- Missing and Repeated NumberCommvault · ONSITE INTERVIEWCommvault interview questions
- Swap Two Integers Without a Temporary VariableOracle · ONSITE INTERVIEWOracle interview questions
- Swap Two Numbers Without a Third VariableInfosys · ONSITE INTERVIEWInfosys interview questions
- Extract a Register Bit FieldApple · ONSITE INTERVIEWApple interview questions
- Add Two Binary StringsPeopleStrong · ONSITE INTERVIEWPeopleStrong interview questions
- Add BinaryMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
- Count Set Bits in an Integer ArrayAkuna Capital · ONSITE INTERVIEWAkuna Capital interview questions
- Check Whether a Byte Is NonzeroZscaler · ONSITE INTERVIEWZscaler interview questions
- Find the Odd-Occurrence ValueExpedia · ONSITE INTERVIEWExpedia interview questions