Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

write an bit into an unsign short array

Status
Not open for further replies.

homeless

Programmer
Nov 6, 2001
20
CH
Hi,

I try to write an bool Value into a member Atribute m_Value.
m_Value is an 32 word a 32 bits,
I think that means:
Code:
 unsigned short* m_Value;
or
 unsigned short m_Value[32];

The Function can look like that:
Code:
void CClass::setBoolValue(bool bValue,unsigned short WordNo,unsigned short BitPos){
	unsigned short WordNr = m_Value[WordNo]; 
}
 
I would expect
Code:
unsigned long m_Value[32];

void CClass::setBoolValue(bool bValue,unsigned short WordNo,unsigned short BitPos){
    unsigned long mask = (1ul<<BitPos);
    m_Value[WordNo] &= ~mask;    // clear a specific bit
    if ( bValue ) {
        m_Value[WordNo] |= mask; // set it, if flag is true
    }
}

--
 
How about
Code:
void CClass::setBoolValue(bool bValue,unsigned short BitPos){
    unsigned short WordNo = BitPos / 32;
    unsigned short bpos = BitPos & 0x1F;
    unsigned long mask = (1ul<<bpos);
    if ( bValue )
        m_Value[WordNo] |= mask; // set it, if flag is true
    else
       m_Value[WordNo] &= ~mask; // clear a specific bit
}
Then you can just set the relevant bit without worrying which word it is in unless you really want 32 words of 32 bits each.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top