bitfield.c

来自「h.248协议源码」· C语言 代码 · 共 108 行

C
108
字号
#ifdef __cplusplus
extern "C" {
#endif



/*

NOTICE:
This document contains information that is proprietary to RADVision LTD.
No part of this publication may be reproduced in any form whatsoever without 
written prior approval by RADVision LTD..

RADVision LTD. reserves the right to revise this publication and make changes 
without obligation to notify any person of such revisions or changes.

*/ 


/****************************************************************************
  
  bitfield.c  --  Bit-field manipulation

  This Comment:  10-Mar-1997

  Abstract:      Misc. routines for bit field manipulation.

  Platforms:     All.
  
  Known Bugs:    None.

****************************************************************************/


#include "bitfield.h"


/*===========================================================================
**  == bitfieldSet() ==                                                    **
**                                                                         **
**  Sets a sequence of bits in an integer to a specified value.            **
**                                                                         **
**  PARAMETERS:                                                            **
**      value      The integer value to work on.                           **
**                                                                         **
**      bitfield   The bit sequence to insert into the integer.            **
**                                                                         **
**      nStartBit  This value specifies the list-segnificant bit index     **
**                 for the bit sequence.  e.g. if nStartBit is set to 1,   **
**                 and nBits is set to 3, the bit sequence will occupy     **
**                 bits 1 through 4.                                       **
**                                                                         **
**      nBits      The number of bits in the bit sequence.                 **
**                                                                         **
**  RETURNS:                                                               **
**      The new integer value.                                             **
**                                                                         **
**=========================================================================*/

RvUint32 bitfieldSet(
	IN  RvUint32	value,
	IN  RvUint32	bitfield,
	IN  int		nStartBit,
	IN  int		nBits)
{
    int mask = (1 << nBits) - 1;
    
    return (value & ~(mask << nStartBit)) + 
           ((bitfield & mask) << nStartBit);
}

/*===========================================================================
**  == bitfieldGet() ==                                                    **
**                                                                         **
**  Retreives the sequence of bits in an integer.                          **
**                                                                         **
**  PARAMETERS:                                                            **
**      value      The integer value to work on.                           **
**                                                                         **
**      nStartBit  This value specifies the list-segnificant bit index     **
**                 of the bit sequence.  e.g. if nStartBit is set to 1,    **
**                 and nBits is set to 3, the bit sequence in bits 1       **
**                 through 4 is used.                                      **
**                                                                         **
**      nBits      The number of bits in the bit sequence.                 **
**                                                                         **
**  RETURNS:                                                               **
**      Returns the bit sequence.  The sequence will occupy bits           **
**      0..(Bits-1).                                                       **
**                                                                         **
**=========================================================================*/

RvUint32 bitfieldGet(
	IN  RvUint32	value,
	IN  int		nStartBit,
	IN  int		nBits)
{
    int mask = (1 << nBits) - 1;

    return (value >> nStartBit) & mask; 
}
#ifdef __cplusplus
}              
#endif



⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?