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

在C语言中使用位运算符获取位值:定位字节差异位的问题

How to Pinpoint Differing Bits Between Two Bytes in C

Great question! You’re already off to a strong start using the XOR operator to spot differing bits—let’s build on that to get the specific bit positions you’re after. Here’s a straightforward, practical approach with code examples:

Core Concept Recap

When you XOR two bytes (byte1 ^ byte2), the resulting value will have a 1 in every bit position where the original bytes differ, and 0 where they match. Our job is to scan this result and map those 1s to their corresponding bit numbers (like your example’s Bit 5 or Bit 8).

Step-by-Step Implementation

  1. Calculate the XOR difference
    First, compute the XOR of your two bytes. If the result is 0, there are no differing bits—you can handle that case upfront to avoid unnecessary work.

    unsigned char byte1 = 0b10101010; // Example input
    unsigned char byte2 = 0b10001110; // Example input
    unsigned char diff = byte1 ^ byte2;
    
  2. Scan each bit position
    Loop through each of the 8 bits in the byte. Since your example uses 1-based numbering (Bit 1 = least significant bit, Bit 8 = most significant bit), we’ll count from 1 to 8. For each position, use a bitmask to check if that bit in diff is set to 1.

  3. Collect and format the result
    Keep track of all differing bit positions, then output them in a clean, user-friendly format (e.g., "Bit 5 and Bit 8 are different").

Full Code Example

#include <stdio.h>

int main() {
    unsigned char byte1 = 0b10101010;
    unsigned char byte2 = 0b10001110;
    unsigned char diff = byte1 ^ byte2;
    
    if (diff == 0) {
        printf("No bits are different.\n");
        return 0;
    }
    
    // Store positions of differing bits (1-based)
    int differing_bits[8];
    int count = 0;
    
    // Check each bit from 1 (LSB) to 8 (MSB)
    for (int i = 0; i < 8; i++) {
        unsigned char mask = 1 << i; // Create bitmask for current position
        if (diff & mask) {
            differing_bits[count++] = i + 1; // Convert to 1-based numbering
        }
    }
    
    // Format the output nicely
    printf("Bit");
    for (int i = 0; i < count; i++) {
        if (i > 0) {
            if (i == count - 1) {
                printf(" and");
            } else {
                printf(",");
            }
        }
        printf(" %d", differing_bits[i]);
    }
    printf(" are different.\n");
    
    return 0;
}

Key Details

  • Bit Numbering: The code uses 1-based numbering to match your example. If you prefer 0-based (standard in most programming contexts), just remove the +1 when storing the bit position.
  • Bitmask Logic: 1 << i shifts the 1 to the i-th position, then diff & mask checks if that bit is set in the XOR result.
  • Output Flexibility: The code handles single, multiple, and no differing bits smoothly—tweak the formatting if you need a different style (e.g., comma-separated without "and" for longer lists).

Test the Example

For the sample values byte1 = 0b10101010 and byte2 = 0b10001110, the XOR result is 0b00100100 (binary), which corresponds to Bit 3 and Bit 6. The code will output:

Bit 3 and Bit 6 are different.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.19 08:48:28