IBR-DTNSuite  0.12
Endianess.cpp
Go to the documentation of this file.
1 /*
2  * Endianess.cpp
3  *
4  * Copyright (C) 2013 IBR, TU Braunschweig
5  *
6  * Written-by: Johannes Morgenroth <morgenroth@ibr.cs.tu-bs.de>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  */
21 
22 #include "ibrdtn/config.h"
23 #include "ibrdtn/data/Endianess.h"
24 
25 namespace dtn
26 {
27  namespace data
28  {
29  uint16_t bswap16(uint16_t x) {
30  union { uint16_t u16; uint8_t v[2]; } ret;
31  ret.v[0] = (uint8_t)(x >> 8);
32  ret.v[1] = (uint8_t) x;
33  return ret.u16;
34  }
35 
36  uint32_t bswap32(uint32_t x) {
37  union { uint32_t u32; uint8_t v[4]; } ret;
38  ret.v[0] = (uint8_t)(x >> 24);
39  ret.v[1] = (uint8_t)(x >> 16);
40  ret.v[2] = (uint8_t)(x >> 8);
41  ret.v[3] = (uint8_t) x;
42  return ret.u32;
43  }
44 
45  uint64_t bswap64(uint64_t x) {
46  union { uint64_t u64; uint8_t v[8]; } ret;
47  ret.v[0] = (uint8_t)(x >> 56);
48  ret.v[1] = (uint8_t)(x >> 48);
49  ret.v[2] = (uint8_t)(x >> 40);
50  ret.v[3] = (uint8_t)(x >> 32);
51  ret.v[4] = (uint8_t)(x >> 24);
52  ret.v[5] = (uint8_t)(x >> 16);
53  ret.v[6] = (uint8_t)(x >> 8);
54  ret.v[7] = (uint8_t) x;
55  return ret.u64;
56  }
57  }
58 }