📄 bitmtesc.c
字号:
/**** ByteReverseWord ***************************************************************
*
* Version & Date
* ------- ----
* 1.0.0, 30/06/1998
*
* Description
* -----------
* given a word (a,b,c,d), reverse the bytes to give the word (d,c,b,a) and return this
*
* Inputs
* ------
* word
* - the word to reverse bytes of
* Return Values
* ------ ------
* int - the reversed word (d,c,b,a)
*
* History (with dates)
* ------- ---- -----
* 1.0.0, 30/06/1998 first release
*
************************************************************************************/
static unsigned int ByteReverseWord( unsigned int word )
{
unsigned int newWord ;
newWord = ( word & 0xFF000000 ) >> 24 ;
newWord += ( word & 0xFF0000 ) >> 8 ;
newWord += ( word & 0xFF00 ) << 8 ;
newWord += ( word & 0xFF ) << 24 ;
return newWord ;
}
/**** ByteWiseMax *******************************************************************
*
* Version & Date
* ------- ----
* 1.0.0, 30/06/1998
*
* Description
* -----------
* given two words (a,b,c,d) and (e,f,g,h), calculate a new word (w,x,y,z) in which
* each byte is the maximum value of the corresponding bytes of the two words given
*
* that is
* (w,x,y,z)=(max(a,e),max(b,f),max(c,g),max(d,h))
*
* Inputs
* ------
* word1, word2
* - the two words to create byte wise maximum value from
* Return Values
* ------ ------
* int - the bytewise maximum value (w,x,y,z)
*
* History (with dates)
* ------- ---- -----
* 1.0.0, 30/06/1998 first release
*
************************************************************************************/
static unsigned int ByteWiseMax( unsigned int word1, unsigned int word2 )
{
unsigned int max ;
max = MAX( ( word1 & 0xFF000000 ), ( word2 & 0xFF000000 ) ) ;
max += MAX( ( word1 & 0xFF0000 ), ( word2 & 0xFF0000 ) ) ;
max += MAX( ( word1 & 0xFF00 ), ( word2 & 0xFF00 ) ) ;
max += MAX( ( word1 & 0xFF ), ( word2 & 0xFF ) ) ;
return max ;
}
/**** LeastSigBit *******************************************************************
*
* Version & Date
* ------- ----
* 1.0.0, 30/06/1998
*
* Description
* -----------
* given a word determine the least significant bit that is set in the word and return
* this or 32 if no bits set (word = 0)
*
* Inputs
* ------
* word
* - the word to find least significant bit of
* Return Values
* ------ ------
* int - the position of least significant bit set
* 32 - no bits were set (word = 0)
*
* History (with dates)
* ------- ---- -----
* 1.0.0, 30/06/1998 first release
*
************************************************************************************/
static unsigned int LeastSigBit( unsigned int word )
{
unsigned int lsb ;
unsigned int mask ;
if( word == 0 ) {
return 32 ;
}
lsb = 0 ;
mask = 0x1 ;
while( ( word & mask ) == 0 ) {
lsb += 1 ;
mask <<= 1 ;
}
return lsb ;
}
/**** MakeBCD ***********************************************************************
*
* Version & Date
* ------- ----
* 1.0.0, 30/06/1998
*
* Description
* -----------
* given an unsigned integer value create it's binary coded decimal (BCD) representation
* so that each nibble of the created word is a value between 0 and 9
*
* Inputs
* ------
* value
* - the integer value to create the BCD of upto a maximum of 99999999
* Return Values
* ------ ------
* unsigned int - the BCD representation of the given value
*
* History (with dates)
* ------- ---- -----
* 1.0.0, 30/06/1998 first release
*
************************************************************************************/
static unsigned int MakeBCD( unsigned int value )
{
unsigned int bcd ;
unsigned int nybbleCounter ;
unsigned int nibble ;
if( value > 0x05F5E0FF ) { /* test not greater than 0x99999999 in BCD */
return 0x99999999 ;
}
bcd = 0 ;
for( nybbleCounter = 0 ; nybbleCounter < 8 ; nybbleCounter += 1 ) {
nibble = value%10 ;
value /= 10 ;
bcd += nibble << nybbleCounter * 4 ;
}
return bcd ;
}
/**** Menu **************************************************************************
*
* Version & Date
* ------- ----
* 1.0.0, 30/06/1998
*
* Description
* -----------
* print the menu of options to the screen (defined in standard way for NextTask
* function and will be called by NextTask)
*
* Inputs
* ------
* numberOptions
* - the number of menu options that should be printed
*
* History (with dates)
* ------- ---- -----
* 1.0.0, 30/06/1998 first release
*
************************************************************************************/
static void Menu( unsigned int numberOptions )
{
if( numberOptions == BITM_OPTIONS ) {
printf( " 1. Test bit manipulation routines.\n" ) ;
}
else {
fprintf( stderr, "[Menu] Error in arguments, aborting.\n\n" ) ;
/* function name given since intended as internal error for programmer */
}
}
/**** MostSigBit ********************************************************************
*
* Version & Date
* ------- ----
* 1.0.0, 30/06/1998
*
* Description
* -----------
* given a word determine the most significant bit that is set in the word and return
* this or 32 if no bits set (word = 0)
*
* Inputs
* ------
* word
* - the word to find most significant bit of
* Return Values
* ------ ------
* int - the position of most significant bit set
* 32 - no bits were set (word = 0)
*
* History (with dates)
* ------- ---- -----
* 1.0.0, 30/06/1998 first release
*
************************************************************************************/
static unsigned int MostSigBit( unsigned int word )
{
unsigned int msb ;
unsigned int mask ;
if( word == 0 ) {
return 32 ;
}
msb = 31 ;
mask = 0x80000000 ;
while( ( word & mask ) == 0 ) {
msb -= 1 ;
mask >>= 1 ;
}
return msb ;
}
/**** PopulationCount ***************************************************************
*
* Version & Date
* ------- ----
* 1.0.0, 30/06/1998
*
* Description
* -----------
* determine number of set bits in the binary representation of the given word (including
* the sign bit)
*
* Inputs
* ------
* word
* - the word to count the number of bits set
* Return Values
* ------ ------
* int - the result of the count on the number of bits set in the given word
*
* History (with dates)
* ------- ---- -----
* 1.0.0, 30/06/1998 first release
*
************************************************************************************/
static unsigned int PopulationCount( int word )
{
unsigned int bits ;
bits = 0 ;
if( word < 0 ) {
word &= 0x7FFFFFFF ;
bits += 1 ;
}
while( word > 0 ) {
if( word & 0x1 ) {
bits += 1 ;
}
word >>= 1 ;
}
return bits ;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -