Skip to main content
added 398 characters in body
Source Link
Olivier Grégoire
  • 14.4k
  • 3
  • 32
  • 56

Swap variablesBit twiddling actions

Swap variables

You might want to swap int and long variables. The usual way is to have a temporary variable:

void swap(int a, int b) {
  int t=a;a=b;b=t;     //  16 bytes
  System.out.printf("a=%d, b=%d%n", a, b);
}

But you can shorten it like this:

void swap(int a, int b) {
  a^=b^(b=a);          // 11 bytes
  System.out.printf("a=%d, b=%d%n", a, b);
}

Even if you reuse a temporary variable, it's just shorter to write that code, no matter the length of each variable name.

Does work with short and byte but at the cost of casting.

Swap the variables so that the min is in a specific variable and the max is in the other

You have a and b and you don't know which is greater. But you want a to contain the lowest and b to contain the greatest.

void swapMinMax(int a, int b) {
  a^=b<a?b^(b=a):0;            // 17 bytes
  System.out.printf("a=%d, b=%d%n", a, b);
}

Swap variables

You might want to swap int and long variables. The usual way is to have a temporary variable:

void swap(int a, int b) {
  int t=a;a=b;b=t;     //  16 bytes
  System.out.printf("a=%d, b=%d%n", a, b);
}

But you can shorten it like this:

void swap(int a, int b) {
  a^=b^(b=a);          // 11 bytes
  System.out.printf("a=%d, b=%d%n", a, b);
}

Even if you reuse a temporary variable, it's just shorter to write that code, no matter the length of each variable name.

Does work with short and byte but at the cost of casting.

Bit twiddling actions

Swap variables

You might want to swap int and long variables. The usual way is to have a temporary variable:

void swap(int a, int b) {
  int t=a;a=b;b=t;     //  16 bytes
  System.out.printf("a=%d, b=%d%n", a, b);
}

But you can shorten it like this:

void swap(int a, int b) {
  a^=b^(b=a);          // 11 bytes
  System.out.printf("a=%d, b=%d%n", a, b);
}

Even if you reuse a temporary variable, it's just shorter to write that code, no matter the length of each variable name.

Does work with short and byte but at the cost of casting.

Swap the variables so that the min is in a specific variable and the max is in the other

You have a and b and you don't know which is greater. But you want a to contain the lowest and b to contain the greatest.

void swapMinMax(int a, int b) {
  a^=b<a?b^(b=a):0;            // 17 bytes
  System.out.printf("a=%d, b=%d%n", a, b);
}
Source Link
Olivier Grégoire
  • 14.4k
  • 3
  • 32
  • 56

Swap variables

You might want to swap int and long variables. The usual way is to have a temporary variable:

void swap(int a, int b) {
  int t=a;a=b;b=t;     //  16 bytes
  System.out.printf("a=%d, b=%d%n", a, b);
}

But you can shorten it like this:

void swap(int a, int b) {
  a^=b^(b=a);          // 11 bytes
  System.out.printf("a=%d, b=%d%n", a, b);
}

Even if you reuse a temporary variable, it's just shorter to write that code, no matter the length of each variable name.

Does work with short and byte but at the cost of casting.