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

如何用位运算符(强制要求)替代if语句重写区间集合成员判断程序

Rewritten Java Code Using Bitwise Operators (No Arrays!)

Got it, let's overhaul this code to use bitwise operators as required. The original approach of storing every number in the interval as an array is really inefficient—especially if your intervals are large (like [1, 100000], that's a massive array for no reason!). Instead, we can work directly with boolean flags for whether x is in interval A or B, then use bitwise operations to compute all the set operations we need.

Key Idea

First, calculate two core boolean values (converted to integers where 1 = true, 0 = false):

  • inA: Is x inside interval A?
  • inB: Is x inside interval B?

Then map set operations directly to bitwise logic:

  • Intersection: inA & inB (bitwise AND) → true only if x is in both intervals
  • Union: inA | inB (bitwise OR) → true if x is in either interval
  • Relative Complement (A\B): inA & (~inB) (AND with NOT) → true only if x is in A but not B
  • Symmetric Difference: inA ^ inB (bitwise XOR) → true if x is in exactly one of the intervals

Full Rewritten Code

import java.util.Scanner;

public class IntervalBitwiseChecker {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        
        // Read interval endpoints and input x
        System.out.print("Input a1: ");
        int a1 = sc.nextInt();
        System.out.print("Input a2: ");
        int a2 = sc.nextInt();
        System.out.print("Input b1: ");
        int b1 = sc.nextInt();
        System.out.print("Input b2: ");
        int b2 = sc.nextInt();
        System.out.print("Input x: ");
        int x = sc.nextInt();
        
        // Convert boolean membership to integer flags (1 = true, 0 = false)
        int inA = (x >= a1 && x <= a2) ? 1 : 0;
        int inB = (x >= b1 && x <= b2) ? 1 : 0;
        
        // Print interval context
        System.out.println("\nInterval A: [" + a1 + ", " + a2 + "]");
        System.out.println("Interval B: [" + b1 + ", " + b2 + "]");
        System.out.println("Enter x: " + x);
        
        // Use bitwise operations to compute each result
        System.out.println("x in A: " + (inA != 0));
        System.out.println("x in B: " + (inB != 0));
        System.out.println("x in A\\B: " + ((inA & ~inB) != 0));
        System.out.println("x in B\\A: " + ((inB & ~inA) != 0));
        System.out.println("x in intersection of A and B: " + ((inA & inB) != 0));
        System.out.println("x in union of A and B: " + ((inA | inB) != 0));
        System.out.println("x in symm. diff. of A and B: " + ((inA ^ inB) != 0));
        
        sc.close();
    }
}

Why This Works

  • No Wasted Memory: We don't store every number in the interval—just check x against the interval bounds directly.
  • Bitwise Alignment: Every set operation maps perfectly to a bitwise operator, which meets the problem's requirement. For example, symmetric difference (in one interval but not both) is exactly what the XOR (^) operator does.
  • Constant Time Efficiency: This code runs in O(1) time no matter how large your intervals are, unlike the original array-based approach which slows down as intervals grow.

Example Test Run

If you input a1=2, a2=4, b1=1, b2=6, x=5, the output will match your example exactly:

Input a1: 2
Input a2: 4
Input b1: 1
Input b2: 6
Input x: 5

Interval A: [2, 4]
Interval B: [1, 6]
Enter x: 5
x in A: false
x in B: true
x in A\B: false
x in B\A: true
x in intersection of A and B: false
x in union of A and B: true
x in symm. diff. of A and B: true

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.28 17:57:45