Translate

Thursday, June 13, 2013

Bitshifting

.: Bit shifting using C Language :.

This is a small, but yet funny program, that demonstrates how to use bit shifting for simple operations using the C Language.

Try to keep this in mind:

  • Move 1 bit to the left (<): multiply by 2
  • Move 1 bit to the right (>): divide by 2
For more information on Bit shifting and Bitwise operations take a look at:
Bitwise operations 



  1 /*
  2  * Flag bits test.
  3  * This program shows how to implement
  4  * flags operations using the eight bits
  5  * of a char variable.
  6  *
  7  * Gabriel Marques
  8  * snortt@gmail.com
  9  *
 10  */
 11 
 12 /*
 13  * Imagine you need to use flags to control
 14  * the state of something. Think of 8 flags,
 15  * for instance, for controlling 8 basic
 16  * functions of a super advanced robot! ;-)
 17  *
 18  * Things that require binary state, such as
 19  * Unit_Status, Engage_Battle, Force_Field, etc
 20  * require ON or OFF control.
 21  *
 22  * You could use variables to control each state,
 23  * but think of the huge waste you would have
 24  * as all of these states are binary values!
 25  *
 26  * char Unity_Status[] = "On"; <-- This would require
 27  * much more space than a simple 0 or 1 as it would
 28  * allocate memory for 3bytes! ('O', 'n', '\n').
 29  * And you would also need to deal with strings
 30  * comparison to test each status!
 31  *
 32  * Now, think of a single bit for each status.
 33  * As a char variable is 1 byte long, we would
 34  * have 8 bits to store information on a single char! :-)
 35  *
 36  * Using bit shifts we could manipulate each bit inside
 37  * our 1 byte (8 bits) char:
 38  *
 39  * You can see the operation and the bit it affects below:
 40  *
 41  *  1<<0 --> 00000001 << 0    00000001 --> Bit 0
 42  *  1<<1 --> 00000001 << 1    00000010 --> Bit 1
 43  *  1<<2 --> 00000001 << 2    00000100 --> Bit 2
 44  *  1<<3 --> 00000001 << 3    00001000 --> Bit 3
 45  *  1<<4 --> 00000001 << 4    00010000 --> Bit 4
 46  *  1<<5 --> 00000001 << 5    00100000 --> Bit 5
 47  *  1<<6 --> 00000001 << 6    01000000 --> Bit 6
 48  *  1<<7 --> 00000001 << 7    10000000 --> Bit 7
 49  *
 50  *
 51  * Let's take a look at a simple example, regarding our
 52  * super advanced robot and its behavior.
 53  *
 54  */
 55 
 56 #include <stdio.h>
 57 #include <stdlib.h>
 58 
 59 /* Definition of each funcionality flag */
 60 const unsigned char UNITY_STATUS             = (1<<0); /* Bit 0 */
 61 const unsigned char UNITY_ENGAGED_FOR_BATTLE = (1<<1); /* Bit 1 */
 62 const unsigned char UNITY_DEFLECTOR_SHIELD   = (1<<2); /* Bit 2 */
 63 const unsigned char UNITY_LASER_CANNON       = (1<<3); /* Bit 3 */
 64 const unsigned char UNITY_MISSLE_ARMED       = (1<<4); /* Bit 4 */
 65 const unsigned char UNITY_MOVE               = (1<<5); /* Bit 5 */
 66 const unsigned char UNITY_AI_SYSTEM_FAILURE  = (1<<6); /* Bit 6 */
 67 const unsigned char UNITY_GENERAL_FAILURE    = (1<<7); /* Bit 7 */
 68 
 69 /*
 70  * Now that we have the flags defined we can manipulate
 71  * them to test if our robot (UNITY) has some modes ON or OFF
 72  * as you will see later inside the main program.
 73  */
 74 
 75 int main() {
 76 
 77     unsigned char unity_control_flags;   /* This controls our robot ;-) */
 78     unity_control_flags = 0;             /* This disables everything */
 79 
 80     /* Let's turn ON our robot */
 81     unity_control_flags |= UNITY_STATUS;
 82 
 83     /* Let's make our unity start its deflector shield */
 84     unity_control_flags |= UNITY_DEFLECTOR_SHIELD;
 85 
 86     /* During a battle, let's suppose our system is damaged */
 87     unity_control_flags |= UNITY_GENERAL_FAILURE;
 88 
 89     /*
 90      * Now, let's test some status flags of our robot
 91      * We do this by testing if some bits are set to 1
 92      * what will result in a TRUE test.
 93      */
 94 
 95     /* Let's see if it is turned on*/
 96     if ( (unity_control_flags & UNITY_STATUS) != 0 )
 97         printf("Unity is on and waiting for instructions!\n");
 98     else
 99         printf("Unity is off!\n");
100 
101     /* Let's test if the unity has its shield on */
102     if ((unity_control_flags & UNITY_DEFLECTOR_SHIELD) != 0)
103         printf("Unity has its deflector shield on!\n");
104     else
105         printf("Deflector shield is down! Unity is vulnerable!\n");
106 
107     /* And we do the same test for a general system failure */
108     if ((unity_control_flags & UNITY_GENERAL_FAILURE) != 0)
109         printf("Warning! General system failure!\n");
110     else
111         printf("Unity system is healthy!\n");
112 
113     /*
114      * Now let's suppose we need to turn some features off
115      * Let's pretend we've just fixed your system failure problem.
116      * We need to reset the corresponding flag!
117      *
118      * We do this by using the flags variable AND NOT the
119      * desired functionality (~)
120      */
121      unity_control_flags &= ~UNITY_GENERAL_FAILURE;
122 
123      /* Let's check if it worked */
124      /* And we do the same test for a general system failure */
125      if ((unity_control_flags & UNITY_GENERAL_FAILURE) != 0)
126         printf("Warning! General system failure!\n");
127      else
128         printf("Unity system is healthy!\n");
129 
130 
131      return 0;
132 }

No comments:

Post a Comment