Xor starting with the significant bit

George Irimiciuc
int a= 21;//10101
int b = 269;//100001101

a^b will do

    10101
100001101
---------
100011000

but I want to do

10101
100001101
---------
001011101

Is there any way to do it without changing the original numbers?

assylias

You can shift a to align it with b on the left. The sample code below works with your example but does not properly handle overflows etc. It should give you a starting point though.

int a = 21;
int b = 269;

int shift = Integer.numberOfLeadingZeros(a) - Integer.numberOfLeadingZeros(b);

int c = (a << shift) ^ b;
System.out.println(Integer.toBinaryString(c)); // 1011101

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Clearing least significant bit

From Dev

Least significant bit mips

From Dev

set most significant bit in C

From Dev

Getting least significant bit in JavaScript

From Dev

Extraction of the least significant bit of a pixel

From Dev

Index of second least significant set bit

From Dev

How to clear most significant bit in byte?

From Dev

Change the least significant bit (LSB) in java

From Dev

Overwriting least-significant bit in a Word

From Dev

Find most significant set bit in a long

From Dev

How to write least significant bit into the buffer?

From Dev

How to Delete Least Significant Bit in Java?

From Dev

Change the least significant bit (LSB) in java

From Dev

fastest way to access the least significant bit of an integer?

From Dev

Efficient least significant set bit of "biginteger" class

From Dev

Comparing the Most Significant Bit of two numbers: ==, <, <=

From Dev

Bitwise XOR: Setting X bit

From Dev

Get the most significant bit from an 8-bit value

From Dev

Get the most significant bit from an 8-bit value

From Dev

Which one-bit integer has more significant bit?

From Dev

how is the most significant bit radix sort more efficient than the least significant bit radix sort?

From Dev

Binary quicksort starting bit position

From Dev

Bitwise xor of two 256-bit integers

From Dev

Ruby XOR bit wise manipulation cryptography exercise

From Dev

NASM XOR signature (32bit)

From Dev

Javascript xor 32bit integer issue

From Dev

Get mosts significant nibble, regardless of int bit length and endianness

From Dev

Performant way to tag a number on JavaScript, using its less significant bit?

From Dev

How can I get the value of the least significant bit in a number?

Related Related

HotTag

Archive