Total members 11796 |It is currently Mon Sep 16, 2019 7:07 am Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





Bit operations - set-get-xor-rotate functions
cpp code
/*****************************************************************************
* *
* --------------------------------- bit.c -------------------------------- *
* *
*****************************************************************************/

#include <string.h>

#include "bit.h"

/*****************************************************************************
* *
* -------------------------------- bit_get ------------------------------- *
* *
*****************************************************************************/

int bit_get(const unsigned char *bits, int pos) {

unsigned char mask;

int i;

/*****************************************************************************
* *
* Set a mask for the bit to get. *
* *
*****************************************************************************/

mask = 0x80;

for (i = 0; i < (pos % 8); i++)
mask = mask >> 1;

/*****************************************************************************
* *
* Get the bit. *
* *
*****************************************************************************/

return (((mask & bits[(int)(pos / 8)]) == mask) ? 1 : 0);

}

/*****************************************************************************
* *
* -------------------------------- bit_set ------------------------------- *
* *
*****************************************************************************/

void bit_set(unsigned char *bits, int pos, int state) {

unsigned char mask;

int i;

/*****************************************************************************
* *
* Set a mask for the bit to set. *
* *
*****************************************************************************/

mask = 0x80;

for (i = 0; i < (pos % 8); i++)
mask = mask >> 1;

/*****************************************************************************
* *
* Set the bit. *
* *
*****************************************************************************/

if (state)
bits[pos / 8] = bits[pos / 8] | mask;
else
bits[pos / 8] = bits[pos / 8] & (~mask);

return;

}

/*****************************************************************************
* *
* -------------------------------- bit_xor ------------------------------- *
* *
*****************************************************************************/

void bit_xor(const unsigned char *bits1, const unsigned char *bits2, unsigned
char *bitsx, int size) {

int i;

/*****************************************************************************
* *
* Compute the bitwise XOR (exclusive OR) of the two buffers. *
* *
*****************************************************************************/

for (i = 0; i < size; i++) {

if (bit_get(bits1, i) != bit_get(bits2, i))
bit_set(bitsx, i, 1);
else
bit_set(bitsx, i, 0);

}

return;

}

/*****************************************************************************
* *
* ----------------------------- bit_rot_left ----------------------------- *
* *
*****************************************************************************/

void bit_rot_left(unsigned char *bits, int size, int count) {

int fbit,
lbit,
i,
j;

/*****************************************************************************
* *
* Rotate the buffer to the left the specified number of bits. *
* *
*****************************************************************************/

if (size > 0) {

for (j = 0; j < count; j++) {

for (i = 0; i <= ((size - 1) / 8); i++) {


/********************************************************************
* *
* Get the bit about to be shifted off the current byte. *
* *
********************************************************************/

lbit = bit_get(&bits[i], 0);

if (i == 0) {

/*****************************************************************
* *
* Save the bit shifted off the first byte for later. *
* *
*****************************************************************/

fbit = lbit;

}

else {

/*****************************************************************
* *
* Set the rightmost bit of the previous byte to the leftmost *
* bit about to be shifted off the current byte. *
* *
*****************************************************************/

bit_set(&bits[i - 1], 7, lbit);

}

/********************************************************************
* *
* Shift the current byte to the left. *
* *
********************************************************************/

bits[i] = bits[i] << 1;

}

/***********************************************************************
* *
* Set the rightmost bit of the buffer to the bit shifted off the *
* first byte. *
* *
***********************************************************************/

bit_set(bits, size - 1, fbit);

}

}

return;

}


Usage example of this bit functions
cpp code
#include <stdio.h>
#include <string.h>

#include "bit.h"

/*****************************************************************************
* *
* --------------------------------- main --------------------------------- *
* *
*****************************************************************************/

int main(int argc, char **argv) {

unsigned char bits1[8],
bits2[8],
bits3[8];

int i;

/*****************************************************************************
* *
* Perform some bit operations using 64-bit buffers. *
* *
*****************************************************************************/


for (i = 0; i < 8; i++) {

bits1[i] = 0x00;
bits2[i] = 0x00;
bits3[i] = 0x00;

}

fprintf(stdout, "Initially\n");

fprintf(stdout, "bits1: %02x %02x %02x %02x %02x %02x %02x %02x\n", bits1[0],
bits1[1], bits1[2], bits1[3], bits1[4], bits1[5], bits1[6], bits1[7]);

fprintf(stdout, "bits2: %02x %02x %02x %02x %02x %02x %02x %02x\n", bits2[0],
bits2[1], bits2[2], bits2[3], bits2[4], bits2[5], bits2[6], bits2[7]);

bit_set(bits1, 15, 1);
bit_set(bits1, 16, 1);
bit_set(bits1, 32, 1);
bit_set(bits1, 63, 1);
bit_set(bits2, 0, 1);
bit_set(bits2, 15, 1);

fprintf(stdout, "After setting bits 15, 16, 32, 63 of bits1 and bits 00, 15 "
"of bits2\n");

fprintf(stdout, "bits1: %02x %02x %02x %02x %02x %02x %02x %02x\n", bits1[0],
bits1[1], bits1[2], bits1[3], bits1[4], bits1[5], bits1[6], bits1[7]);

fprintf(stdout, "bits2: %02x %02x %02x %02x %02x %02x %02x %02x\n", bits2[0],
bits2[1], bits2[2], bits2[3], bits2[4], bits2[5], bits2[6], bits2[7]);

fprintf(stdout, "Bit 63 of bits1 is %d\n", bit_get(bits1, 63));
fprintf(stdout, "Bit 62 of bits1 is %d\n", bit_get(bits1, 62));
fprintf(stdout, "Bit 00 of bits2 is %d\n", bit_get(bits2, 0));
fprintf(stdout, "Bit 01 of bits2 is %d\n", bit_get(bits2, 1));

bit_xor(bits1, bits2, bits3, 32);

fprintf(stdout, "bits3 is bits1 XOR bits2 (32 bits)\n");

fprintf(stdout, "bits3: %02x %02x %02x %02x %02x %02x %02x %02x\n", bits3[0],
bits3[1], bits3[2], bits3[3], bits3[4], bits3[5], bits3[6], bits3[7]);

bit_xor(bits1, bits2, bits3, 64);

fprintf(stdout, "bits3 is bits1 XOR bits2 (64 bits)\n");

fprintf(stdout, "bits3: %02x %02x %02x %02x %02x %02x %02x %02x\n", bits3[0],
bits3[1], bits3[2], bits3[3], bits3[4], bits3[5], bits3[6], bits3[7]);

bit_rot_left(bits1, 64, 1);

fprintf(stdout, "After rotating bits1 left x 1 (64 bits)\n");

fprintf(stdout, "bits1: %02x %02x %02x %02x %02x %02x %02x %02x\n", bits1[0],
bits1[1], bits1[2], bits1[3], bits1[4], bits1[5], bits1[6], bits1[7]);

bit_rot_left(bits2, 64, 1);

fprintf(stdout, "After rotating bits2 left x 1 (64 bits)\n");

fprintf(stdout, "bits2: %02x %02x %02x %02x %02x %02x %02x %02x\n", bits2[0],
bits2[1], bits2[2], bits2[3], bits2[4], bits2[5], bits2[6], bits2[7]);

bit_rot_left(bits2, 16, 7);

fprintf(stdout, "After rotating bits2 left x 7 (16 bits)\n");

fprintf(stdout, "bits2: %02x %02x %02x %02x %02x %02x %02x %02x\n", bits2[0],
bits2[1], bits2[2], bits2[3], bits2[4], bits2[5], bits2[6], bits2[7]);

bit_rot_left(bits2, 8, 2);

fprintf(stdout, "After rotating bits2 left x 2 (8 bits)\n");

fprintf(stdout, "bits2: %02x %02x %02x %02x %02x %02x %02x %02x\n", bits2[0],
bits2[1], bits2[2], bits2[3], bits2[4], bits2[5], bits2[6], bits2[7]);

for (i = 0; i < 8; i++) {



bits2[i] = 0x00;



}



bit_set(bits2, 0, 1);

bit_set(bits2, 3, 1);

bit_set(bits2, 8, 1);

bit_set(bits2, 27, 1);


fprintf(stdout, "After clearing and setting bits 0, 3, 8, 27 of bits2\n");



fprintf(stdout, "bits2: %02x %02x %02x %02x %02x %02x %02x %02x\n", bits2[0],

bits2[1], bits2[2], bits2[3], bits2[4], bits2[5], bits2[6], bits2[7]);



bit_rot_left(bits2, 11, 6);



fprintf(stdout, "After rotating bits2 left x 6 (11 bits)\n");

fprintf(stdout, "bits2: %02x %02x %02x %02x %02x %02x %02x %02x\n", bits2[0],
bits2[1], bits2[2], bits2[3], bits2[4], bits2[5], bits2[6], bits2[7]);


for (i = 0; i < 8; i++) {



bits2[i] = 0x00;



}



bit_set(bits2, 0, 1);

bit_set(bits2, 3, 1);

bit_set(bits2, 8, 1);

bit_set(bits2, 27, 1);



fprintf(stdout, "After clearing and setting bits 0, 3, 8, 27 of bits2\n");



fprintf(stdout, "bits2: %02x %02x %02x %02x %02x %02x %02x %02x\n", bits2[0],

bits2[1], bits2[2], bits2[3], bits2[4], bits2[5], bits2[6], bits2[7]);



bit_rot_left(bits2, 28, 4);



fprintf(stdout, "After rotating bits2 left x 4 (28 bits)\n");



fprintf(stdout, "bits2: %02x %02x %02x %02x %02x %02x %02x %02x\n", bits2[0],

bits2[1], bits2[2], bits2[3], bits2[4], bits2[5], bits2[6], bits2[7]);



return 0;

}


Bit.h
cpp code
/*****************************************************************************
* *
* --------------------------------- bit.h -------------------------------- *
* *
*****************************************************************************/

#ifndef BIT_H
#define BIT_H

/*****************************************************************************
* *
* --------------------------- Public Interface --------------------------- *
* *
*****************************************************************************/

int bit_get(const unsigned char *bits, int pos);

void bit_set(unsigned char *bits, int pos, int state);

void bit_xor(const unsigned char *bits1, const unsigned char *bits2, unsigned
char *bitsx, int size);

void bit_rot_left(unsigned char *bits, int size, int count);

#endif


Code author
Code:
"Mastering Algorithms with C"  by Kyle Loudon





Author:
Mastermind
User avatar Posts: 2707
Have thanks: 74 time
Post new topic Reply to topic  [ 1 post ] 

  Related Posts  to : Bit operations-set-get-xor-rotate on bits arrays
 Ensuring integer are written as 32 bits     -  
 Transform Rotate Filter     -  
 Rotate Image with specific angle     -  
 rotate sphere in a circle with light     -  
 C++ Boolean Operations     -  
 Operations on Sucxent++     -  
 Character Operations     -  
 Matrix Operations     -  
 Input-Output Operations     -  
 solve the complex numbers and do operations on it     -  



Topic Tags

C++ Arrays






Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com