C language
How to move the position of a certain bit within a byte ( coding the C language )
Feb 16th
You want to move bits inside a byte? Unfortunately, there aren’t any standard functions that deal with these sort of issues so we will have to use some smart and easy hacks.
First of all we need to use the tilde operator (~). Tilde is a bitwise not operator; this practically means that applying it to a certain operand’s binary value converts all the 1s to 0s and viceversa (~b0001 0010 = b1110 1101).
So what’s the use of tilde in our little program. We’re going to use it to create a mask that will delete (make 0) the bit of a certain location, that is, the bit we’re trying to move to another position, but we’ll also delete the bit from the destination position.
For example we have the byte 0xc3, and we need to move the bit from position 1 to position 5. Tilde will create a mask that will be used to delete the bit in position 1 (all the mask’s bits are 1s besides the bit at the position we’re trying to delete from which is of value 0) and position 5 (see line 14 and 15).
The mask achieves the delete of the bit by being AND-ed with our byte. Then we will OR the byte that we deleted the bits from (oldvalue) with another mask that contains the bit – of which value we read at line 12 with 0×01 & ((*our)>>indexold) – we will be writing at position 5. This new mask is of course made of all zeros besides the bit at position 5 which is of value valuetochange(=1 in our case) :
// example 1 (move bit from position 1 to position 5)
// 1100 0011
// ^ ^ BYTE MASK DEL BIT
// bit to be changed position has value 1
// 1100 0011 & 1111 1101 = 1100 0001
// 1100 0001 & 1101 1111 = 1100 0001
// DEL BIT MASK NEW BYTE
// 1100 0001 | 0010 0000 = 1110 0001
So practically we begin by registering the value of the bit we want to move in the valuetochange variable. We then delete the bits from both the current position of the bit we want to change the position of and its future position. Finally, we write the value from valuetochange into out byte. This is all achieved by:
// valuetochange = 0x01 & ((*our)>>indexold); // get the value of the bit to be moved oldvalue = (*our) & (~(1<<(indexold))); // del the bit from position indexold oldvalue = oldvalue & (~(1<<(indexnew))); // del the bit from position indexnew newvalue = oldvalue | (valuetochange<<(indexnew)); // write bit in new position (indexnew) //
Here’s all the code. I’ve compiled it using Visual Studio 2010, but earlier version should suffice for this little project.
#include <stdio.h>
#include <malloc.h>
typedef unsigned __int8 byte;
byte move(byte* our, int indexold, int indexnew)
{
byte oldvalue;
byte newvalue;
byte valuetochange;
valuetochange = 0x01 & ((*our)>>indexold); // get the value of the bit to be moved
printf("value to change : %d\n", valuetochange);
oldvalue = (*our) & (~(1<<(indexold))); // del the bit from position indexold
oldvalue = oldvalue & (~(1<<(indexnew))); // del the bit from position indexnew
printf("deleted: %x\n", oldvalue);
newvalue = oldvalue | (valuetochange<<(indexnew)); // write bit in new position (indexnew)
return newvalue;
}
int main()
{
byte* example_byte;
byte* new_byte;
example_byte = (byte*)malloc(sizeof(byte));
new_byte = (byte*)malloc(sizeof(byte));
*example_byte = 0xc3; // hex 0xc3 = binary 1100 0011
printf("\n");
//*****************************************************
// example 1 (move bit from position 1 to position 5)
// example_byte 1100 0011
// ^ ^
// memorize bit -> valuetochange = 0x01 & ((*our)>>indexold) = 1
// 1100 0011 & 1111 1101 = 1100 0001 delete bit from oldindex (1)
// 1100 0001 & 1101 1111 = 1100 0001 delete bit from newindex (5)
// new_byte 1100 0001 | 0010 0000 = 1110 0001
*new_byte = move(example_byte, 1, 5);
printf("old byte : %x\n", *example_byte); // 0xc3 (1100 0011)
printf("new byte : %x\n", *new_byte); // 0xe1 (1110 0001)
printf("\n");
//*****************************************************
// example 2 (move bit from position 6 to position 3)
// example_byte 1100 0011
// ^ ^
// memorize bit -> valuetochange = 0x01 & ((*our)>>indexold) = 1
// 1100 0011 & 1011 1111 = 1000 0011 delete bit from oldindex (6)
// 1000 0011 & 1111 0111 = 1000 0011 delete bit ftom newindex (3)
// new_byte 1000 0011 | 0000 1000 = 1000 1011
*new_byte = move(example_byte, 6, 3);
printf("old byte : %x\n", *example_byte); // 0xc3 (1100 0011)
printf("new byte : %x\n", *new_byte); // 0x8b (1000 1011)
printf("\n");
//*****************************************************
// example 3 (move bit from position 2 to position 6)
// example_byte 1100 0011
// ^ ^
// memorize bit -> valuetochange = 0x01 & ((*our)>>indexold) = 0
// 1100 0011 & 1111 1011 = 1100 0011 delete bit from oldindex (2)
// 1100 0011 & 1011 1111 = 1000 0011 delete bit from oldindex (6)
// new_byte 1000 0011 | 0000 0000 = 1000 0011
*new_byte = move(example_byte, 2, 6);
printf("old byte : %x\n", *example_byte); // 0xc3 (1100 0011)
printf("new byte : %x\n", *new_byte); // 0x83 (1000 0011)
printf("\n");
//*****************************************************
// example 4 (move bit from position 2 to position 4)
// example_byte 1100 0011
// ^ ^
// memorize bit -> valuetochange = 0x01 & ((*our)>>indexold) = 0
// 1100 0011 & 1111 1011 = 1100 0011 delete bit from oldindex (2)
// 1100 0011 & 1110 1111 = 1100 0011 delete bit from oldindex (4)
// new_byte 1100 0011 | 0000 0000 = 1100 0011
*new_byte = move(example_byte, 2, 4);
printf("old byte : %x\n", *example_byte); // 0xc3 (1100 0011)
printf("new byte : %x ", *new_byte); // 0xc3 (1100 0011)
printf("\n");
free(new_byte);
free(example_byte);
return 0;
}








