JWS C Library
C language utility library
bits.c
Go to the documentation of this file.
00001 /*
00002  * This work is licensed under a Creative Commons 
00003  * Attribution-Noncommercial-Share Alike 3.0 United States License.
00004  * 
00005  *    http://creativecommons.org/licenses/by-nc-sa/3.0/us/
00006  * 
00007  * You are free:
00008  * 
00009  *    to Share - to copy, distribute, display, and perform the work
00010  *    to Remix - to make derivative works
00011  * 
00012  * Under the following conditions:
00013  * 
00014  *    Attribution. You must attribute the work in the manner specified by the
00015  *    author or licensor (but not in any way that suggests that they endorse you
00016  *    or your use of the work).
00017  * 
00018  *    Noncommercial. You may not use this work for commercial purposes.
00019  * 
00020  *    Share Alike. If you alter, transform, or build upon this work, you may
00021  *    distribute the resulting work only under the same or similar license to
00022  *    this one.
00023  * 
00024  * For any reuse or distribution, you must make clear to others the license
00025  * terms of this work. The best way to do this is by including this header.
00026  * 
00027  * Any of the above conditions can be waived if you get permission from the
00028  * copyright holder.
00029  * 
00030  * Apart from the remix rights granted under this license, nothing in this
00031  * license impairs or restricts the author's moral rights.
00032  */
00033 
00034 
00046 #include <jwsc/config.h>
00047 
00048 #include <stdlib.h>
00049 #include <stdio.h>
00050 #include <inttypes.h>
00051 
00052 #include "jwsc/base/bits.h"
00053 
00054 
00068 void bswap_u16(uint16_t* x)
00069 {
00070     *x = (*x>>8) | 
00071          (*x<<8);
00072 }
00073 
00074 
00081 void bswap_u32(uint32_t* x)
00082 {
00083     *x = (*x>>24) | 
00084          ((*x<<8) & 0x00FF0000) |
00085          ((*x>>8) & 0x0000FF00) |
00086          (*x<<24);
00087 }
00088 
00089 
00096 void bswap_u64(uint64_t* x)
00097 {
00098     uint64_t a = 0xFF;
00099 
00100     *x = (*x>>56) | 
00101          ((*x<<40) & (a<<48)) |
00102          ((*x<<24) & (a<<40)) |
00103          ((*x<<8)  & (a<<32)) |
00104          //((*x<<40) & 0x00FF000000000000) |
00105          //((*x<<24) & 0x0000FF0000000000) |
00106          //((*x<<8)  & 0x000000FF00000000) |
00107          ((*x>>8)  & 0x00000000FF000000) |
00108          ((*x>>24) & 0x0000000000FF0000) |
00109          ((*x>>40) & 0x000000000000FF00) |
00110          (*x<<56);
00111 }
00112