Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

8/16 bit value reverting function (11000000 to 00000011)

Fast value reverting function for 8-bit char and 16-bit integer, it uses a lookup table for each nibble (4-bits).


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
/*****************************************************************************
** Function name:   reverse_byte    
**
** Descriptions:    reverts the bits of a byte b0->b7, b1->b6, b2->b5 etc
**                  
** parameters:      unsigned char input_byte : the values that will be inverted 
** Returned value:  unsigned char : the reverted result 
** 
*****************************************************************************/
unsigned char reverse_byte (unsigned char input_byte)
{
  const unsigned char lookup[16]={0,8,4,12,2,10,6,14,1,9,5,13,3,11,7,15}; // depending on the compiler use the proper keyword to make this a flash (ROM) variable
 
  return (lookup[input_byte & 15]<<4) | (lookup[input_byte>>4]);
}
 
 
/*****************************************************************************
** Function name:   reverse_int 
**
** Descriptions:    reverts the bits of a byte b0->b15, b1->b14, b2->b13 etc
**                  
** parameters:      unsigned int input_int : the values that will be inverted   
** Returned value:  unsigned int : the reverted result
** 
*****************************************************************************/
unsigned int reverse_int (unsigned int input_int)
{
  const unsigned char lookup[16]={0,8,4,12,2,10,6,14,1,9,5,13,3,11,7,15}; // depending on the compiler use the proper keyword to make this a flash (ROM) variable
 
  return (lookup[input_int & 15]<<12) | (lookup[(input_int>>4) & 15]<<8) | (lookup[(input_int>>8) & 15]<<4) | (lookup[(input_int>>12)]);
}



Alex

Comments

There are no comments to display.

Part and Inventory Search

Blog entry information

Author
alexan_e
Read time
1 min read
Views
814
Last update

More entries in AVR

More entries from alexan_e

Share this entry

Back
Top