You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

关于十进制转二进制代码中0x1作用的技术咨询

Understanding the 0x1 in Your Decimal-to-Binary Conversion Code

Let me break down exactly what that 0x1 is doing here—bitwise operations can feel opaque at first, so it’s totally reasonable to ask about this!

First: What is 0x1?

0x1 is a hexadecimal literal representing the number 1. Since your code assumes a 16-bit integer size, in binary this translates to:

0000000000000001

In short: it’s a value where only the least significant (rightmost) bit is set to 1, and all other bits are 0.

The Magic of u_i & 0x1 (Bitwise AND)

The & operator performs a bitwise AND between two integers. The rule for bitwise AND is simple:

For each pair of corresponding bits in the two numbers, the result is 1 only if both bits are 1; otherwise, it’s 0.

When you AND u_i with 0x1, every bit in u_i except the least significant bit gets ANDed with 0 (from 0x1), which turns those bits to 0. The only bit that matters is u_i’s least significant bit, which gets ANDed with 1—so it stays exactly as it was (1 stays 1, 0 stays 0).

This operation’s sole purpose is to extract the current least significant bit of u_i and store it in the array b[j].

How This Fits Into Your Full Conversion Flow

Let’s walk through the loop logic to see how 0x1 works with the right shift (>>):

  1. You start with u_i set to your input number (cast to unsigned int to handle negative values correctly with logical shifts).
  2. For each iteration from j=15 down to j=0:
    • b[j] = u_i & 0x1: Grab the rightmost bit of u_i and save it in the array.
    • u_i = u_i >> 1: Shift all bits of u_i to the right by 1. For unsigned integers, this is a logical shift—zeros fill in the leftmost (most significant) position. This moves the next bit to the right into the least significant spot, ready to be extracted in the next loop.
  3. By the end of the loop, your array b holds the binary representation of the input number, with b[0] being the most significant bit and b[15] the least significant bit. Printing the array from start to end gives you the correct binary string.

Example Walkthrough (Input = 5)

Let’s take a concrete example where input i=5 (binary 0000000000000101):

  • First iteration (j=15): u_i=5 → 5 & 0x1 = 1 → b[15]=1; u_i shifts right to 2 (0000000000000010).
  • Second iteration (j=14): u_i=2 → 2 & 0x1 = 0 → b[14]=0; u_i shifts right to 1 (0000000000000001).
  • Third iteration (j=13): u_i=1 → 1 & 0x1 =1 → b[13]=1; u_i shifts right to 0.
  • All remaining iterations (j=12 to 0): u_i=0 → 0 &0x1=0 → b[j]=0.

The final array b is 0000000000000101, which prints as the correct binary representation of 5.

Quick Note on Unsigned Int

Casting i to unsigned int is important here because signed integers use arithmetic right shifts (which fill the left with the sign bit for negative numbers), leading to incorrect binary outputs for negative values. Using unsigned ensures logical shifts, so we get the raw binary (two’s complement) representation of negative numbers correctly.

内容的提问来源于stack exchange,提问作者Paul

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 03:39:53