meta data for this page
  •  

Network programming @ Noppa | @ wiki - Updated: — Jussi Laakkonen 2011/06/20 13:22

Bit shifting example with 8bit integer

This example shows how to use bit shifting with binary or operations to construct a single 8 bit integer to contain packed information. Read about Bitwise operations in C. Following example shows how to create an integer which should have

+-+-+-+-+-+-+-+-+
|1 0 0|0 1|1 0 0|
+-+-+-+-+-+-+-+-+

structure and content. Note that there are no types for 3bit or 2bit integers, use e.g. 8bit unsigned integer and watch carefully that the value contained by the integers do not exceed the maximum of the reserved area in the resulting integer, e.g. first integer cannot be bigger than 7 (2²+2¹+2⁰).

Put values into 8bit integer

Example in C:

// In binary the value is 0000 0100
uint8_t first = 4;
 
// Take only 3 bits from first variable into beginning of result
// -> shift 5 bits left, result is 1000 0000
/*
      FIRST                                       RESULT
+-+-+-+-+-+-+-+-+                            +-+-+-+-+-+-+-+-+
|0 0 0 0 0 1 0 0| BITSHIFT LEFT BY 5 BITS -> |1 0 0 0 0 0 0 0|
+-+-+-+-+-+-+-+-+                            +-+-+-+-+-+-+-+-+
*/
uint8_t result = first<<5;
 
// In binary the value is 0000 0001
uint8_t second = 1;
 
// Take 2 bits from second and add them to result as 4th and 
// 5th bit with binary bitwise inclusive OR
/*
  RESULT BEFORE          SECOND<<3         RESULT AFTER
+-+-+-+-+-+-+-+-+    +-+-+-+-+-+-+-+-+   +-+-+-+-+-+-+-+-+
|1 0 0 0 0 0 0 0| OR |0 0 0 0 1 0 0 0| = |1 0 0 0 1 0 0 0|
+-+-+-+-+-+-+-+-+    +-+-+-+-+-+-+-+-+   +-+-+-+-+-+-+-+-+
*/
result |= second<<3;
 
// In binary the value is 0000 0100
uint8_t third = 4;
 
// Use binary bitwise inclusive OR to put the rest into result,
// variable third should have no bigger value than 0000 0111
result |= third;
/*
   R E S U L T
+-+-+-+-+-+-+-+-+
|1 0 0 0 1 1 0 0|
+-+-+-+-+-+-+-+-+
*/

Extract values from 8bit integer

To extract these values from the same result variable:

/*
   R E S U L T
+-+-+-+-+-+-+-+-+
|1 0 0 0 1 1 0 0|
+-+-+-+-+-+-+-+-+
*/
 
// Bitmask for second 2 bit value is 0000 0011 
int8_t bitmask_second = 3;
 
// Bitmask for third 3 bit value is 0000 0111
int8_t bitmask_third = 7;
 
// No need for bitmask for first, shift bytes right
/*
     RESULT                                         FIRST
+-+-+-+-+-+-+-+-+                             +-+-+-+-+-+-+-+-+
|1 0 0 0 1 1 0 0| BITSHIFT RIGHT BY 5 BITS -> |0 0 0 0 0 1 0 0|
+-+-+-+-+-+-+-+-+                             +-+-+-+-+-+-+-+-+
*/
 
int8_t first3b = result >> 5;
 
// Shift bytes right and get the value by using binary bitwise 
// AND with proper bitmask
/*
   RESULT >> 3             BIT MASK            SECOND
+-+-+-+-+-+-+-+-+     +-+-+-+-+-+-+-+-+   +-+-+-+-+-+-+-+-+
|0 0 0 1 0 0 0 1| AND |0 0 0 0 0 0 1 1| = |0 0 0 0 0 0 0 1|
+-+-+-+-+-+-+-+-+     +-+-+-+-+-+-+-+-+   +-+-+-+-+-+-+-+-+
*/
 
int8_t second2b = (result >> 3) & bitmask_second;
 
// Get the value by using binary bitwise AND with proper bitmask
/*
     RESULT               BIT MASK              THIRD
+-+-+-+-+-+-+-+-+     +-+-+-+-+-+-+-+-+   +-+-+-+-+-+-+-+-+
|1 0 0 0 1 1 0 0| AND |0 0 0 0 0 1 1 1| = |0 0 0 0 0 1 0 0|
+-+-+-+-+-+-+-+-+     +-+-+-+-+-+-+-+-+   +-+-+-+-+-+-+-+-+
*/
 
int8_t third3b = result & bitmask_third;