"Big Endian" and "Little Endian" Formats for Integers

Created on 2021-12-08T20:09:08-06:00

Return to the Index

This card pertains to a resource available on the internet.

This card can also be read via Gemini.

XORing byte pairs 3 times swaps their endian in-place

int swap_endian(int x) { 
  char *c; 
  c = (char *)&x; 
  /* XOR byte pairs 3 times swaps them in place */ 
  c[0]=c[0]^c[3]; c[3]=c[0]^c[3]; c[0]=c[0]^c[3];  
  c[1]=c[1]^c[2]; c[2]=c[1]^c[2]; c[1]=c[1]^c[2]; 
  return(x); 
}