Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I swap endian-ness (byte order) of a variable in javascript

I am receiving and sending a decimal representation of two little endian numbers. I would like to:

  • shift one variable 8 bits left
  • OR them
  • shift a variable number of bits
  • create 2 8 bit numbers representing the first and second half of the 16 bit number.

javascript (according to https://developer.mozilla.org/en/JavaScript/Reference/Operators/Bitwise_Operators) uses big endian representation when shifting...

endianness is a bit foreign to me (I am only 90 percent sure that my outlined steps are what i want.) so swapping is a bit dizzying. please help! I only really need to know how to swap the order in an efficient manner. (I can only think of using a for loop on a toString() return value)

like image 650
griotspeak Avatar asked Mar 16 '11 02:03

griotspeak


People also ask

Does endianness affect bit order?

Bit order usually follows the same endianness as the byte order for a given computer system. That is, in a big endian system the most significant bit is stored at the lowest bit address; in a little endian system, the least significant bit is stored at the lowest bit address.

What is endian swap?

In little-endian style, the bytes are written from left to right in increasing significance. In big-endian style, the bytes are written from left to right in decreasing significance. The swapbytes function swaps the byte ordering in memory, converting little endian to big endian (and vice versa).

Does endianness affect array order?

Endianness only affects the order of bytes (not bits) in a data word. Since a char is always one-byte in size, you don't have to do any adjustments to them. The standard library function ntohl changes the byte order of an unsigned integer ( l ) from network byte order ( n ) to host byte order ( h ).

What is Javascript endianness?

Endian and endianness (or "byte-order") describe how computers organize the bytes that make up numbers. Each memory storage location has an index or address. Every byte can store an 8-bit number (i.e. between 0x00 and 0xff ), so you must reserve more than one byte to store a larger number.


1 Answers

function swap16(val) {
    return ((val & 0xFF) << 8)
           | ((val >> 8) & 0xFF);
}

Explanation:

  1. Let's say that val is, for example, 0xAABB.
  2. Mask val to get the LSB by &ing with 0xFF: result is 0xBB.
  3. Shift that result 8 bits to the left: result is 0xBB00.
  4. Shift val 8 bits to the right: result is 0xAA (the LSB has "dropped off" the right-hand side).
  5. Mask that result to get the LSB by &ing with 0xFF: result is 0xAA.
  6. Combine the results from steps 3 and step 5 by |ing them together:
    0xBB00 | 0xAA is 0xBBAA.

function swap32(val) {
    return ((val & 0xFF) << 24)
           | ((val & 0xFF00) << 8)
           | ((val >> 8) & 0xFF00)
           | ((val >> 24) & 0xFF);
}

Explanation:

  1. Let's say that val is, for example, 0xAABBCCDD.
  2. Mask val to get the LSB by &ing with 0xFF: result is 0xDD.
  3. Shift that result 24 bits to the left: result is 0xDD000000.
  4. Mask val to get the second byte by &ing with 0xFF00: result is 0xCC00.
  5. Shift that result 8 bits to the left: result is 0xCC0000.
  6. Shift val 8 bits to the right: result is 0xAABBCC (the LSB has "dropped off" the right-hand side).
  7. Mask that result to get the second byte by &ing with 0xFF00: result is 0xBB00.
  8. Shift val 24 bits to the right: result is 0xAA (everything except the MSB has "dropped off" the right-hand side).
  9. Mask that result to get the LSB by &ing with 0xFF: result is 0xAA.
  10. Combine the results from steps 3, 5, 7 and 9 by |ing them together:
    0xDD000000 | 0xCC0000 | 0xBB00 | 0xAA is 0xDDCCBBAA.
like image 190
LukeH Avatar answered Oct 22 '22 14:10

LukeH