如何用位运算符(强制要求)替代if语句重写区间集合成员判断程序
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: Isxinside interval A?inB: Isxinside interval B?
Then map set operations directly to bitwise logic:
- Intersection:
inA & inB(bitwise AND) → true only ifxis in both intervals - Union:
inA | inB(bitwise OR) → true ifxis in either interval - Relative Complement (A\B):
inA & (~inB)(AND with NOT) → true only ifxis in A but not B - Symmetric Difference:
inA ^ inB(bitwise XOR) → true ifxis 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
xagainst 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

