qca
bit_ops.cpp
Go to the documentation of this file.00001 /* 00002 Copyright (C) 1999-2007 The Botan Project. All rights reserved. 00003 00004 Redistribution and use in source and binary forms, for any use, with or without 00005 modification, is permitted provided that the following conditions are met: 00006 00007 1. Redistributions of source code must retain the above copyright notice, this 00008 list of conditions, and the following disclaimer. 00009 00010 2. Redistributions in binary form must reproduce the above copyright notice, 00011 this list of conditions, and the following disclaimer in the documentation 00012 and/or other materials provided with the distribution. 00013 00014 THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) "AS IS" AND ANY EXPRESS OR IMPLIED 00015 WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 00016 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE DISCLAIMED. 00017 00018 IN NO EVENT SHALL THE AUTHOR(S) OR CONTRIBUTOR(S) BE LIABLE FOR ANY DIRECT, 00019 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 00020 BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 00021 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 00022 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 00023 OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 00024 ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00025 */ 00026 // LICENSEHEADER_END 00027 namespace QCA { // WRAPNS_LINE 00028 /************************************************* 00029 * Bit/Word Operations Source File * 00030 * (C) 1999-2007 The Botan Project * 00031 *************************************************/ 00032 00033 } // WRAPNS_LINE 00034 #include <botan/bit_ops.h> 00035 namespace QCA { // WRAPNS_LINE 00036 00037 namespace Botan { 00038 00039 /************************************************* 00040 * XOR arrays together * 00041 *************************************************/ 00042 void xor_buf(byte data[], const byte mask[], u32bit length) 00043 { 00044 while(length >= 8) 00045 { 00046 data[0] ^= mask[0]; data[1] ^= mask[1]; 00047 data[2] ^= mask[2]; data[3] ^= mask[3]; 00048 data[4] ^= mask[4]; data[5] ^= mask[5]; 00049 data[6] ^= mask[6]; data[7] ^= mask[7]; 00050 data += 8; mask += 8; length -= 8; 00051 } 00052 for(u32bit j = 0; j != length; ++j) 00053 data[j] ^= mask[j]; 00054 } 00055 00056 void xor_buf(byte out[], const byte in[], const byte mask[], u32bit length) 00057 { 00058 while(length >= 8) 00059 { 00060 out[0] = in[0] ^ mask[0]; out[1] = in[1] ^ mask[1]; 00061 out[2] = in[2] ^ mask[2]; out[3] = in[3] ^ mask[3]; 00062 out[4] = in[4] ^ mask[4]; out[5] = in[5] ^ mask[5]; 00063 out[6] = in[6] ^ mask[6]; out[7] = in[7] ^ mask[7]; 00064 in += 8; out += 8; mask += 8; length -= 8; 00065 } 00066 for(u32bit j = 0; j != length; ++j) 00067 out[j] = in[j] ^ mask[j]; 00068 } 00069 00070 /************************************************* 00071 * Return true iff arg is 2**n for some n > 0 * 00072 *************************************************/ 00073 bool power_of_2(u64bit arg) 00074 { 00075 if(arg == 0 || arg == 1) 00076 return false; 00077 if((arg & (arg-1)) == 0) 00078 return true; 00079 return false; 00080 } 00081 00082 /************************************************* 00083 * Return the index of the highest set bit * 00084 *************************************************/ 00085 u32bit high_bit(u64bit n) 00086 { 00087 for(u32bit count = 64; count > 0; --count) 00088 if((n >> (count - 1)) & 0x01) 00089 return count; 00090 return 0; 00091 } 00092 00093 /************************************************* 00094 * Return the index of the lowest set bit * 00095 *************************************************/ 00096 u32bit low_bit(u64bit n) 00097 { 00098 for(u32bit count = 0; count != 64; ++count) 00099 if((n >> count) & 0x01) 00100 return (count + 1); 00101 return 0; 00102 } 00103 00104 /************************************************* 00105 * Return the number of significant bytes in n * 00106 *************************************************/ 00107 u32bit significant_bytes(u64bit n) 00108 { 00109 for(u32bit j = 0; j != 8; ++j) 00110 if(get_byte(j, n)) 00111 return 8-j; 00112 return 0; 00113 } 00114 00115 /************************************************* 00116 * Return the Hamming weight of n * 00117 *************************************************/ 00118 u32bit hamming_weight(u64bit n) 00119 { 00120 u32bit weight = 0; 00121 for(u32bit j = 0; j != 64; ++j) 00122 if((n >> j) & 0x01) 00123 ++weight; 00124 return weight; 00125 } 00126 00127 } 00128 } // WRAPNS_LINE
KDE 4.4 API Reference