const: happen in running
#define: happen at complie time, before program running
const int i = ______ ->have to write a value
const char *p; ---> const
char* const q = name; ---->const, and q points name.
int& foo(int& R) RENAME, foo(i) = i;
Monday, April 18, 2011
Note about & and |
A - B in C language is equal to A+( ~B + 1 ), so ( ~B + 1 ) + B = 0.
Examples:
#include <cstdio>
using namespace std;
int main(void){
char A = 0x53;
char B = 0x9A;
char C;
C = A & B;
printf("%X\n", C);
C = A | B;
printf("%X\n", C);
C = ~A;
printf("%X\n", C);
C = ~B;
printf("%X\n", C);
C = A + (~A+1); // C = A - A;
printf("%X\n", C);
C = A ^ B;
printf("%X\n", C);
C = C ^ B;
printf("%X\n", C);
printf("Shifts.......\n");
C = A << 1;
printf("%X\n", C);
C = B << 1;
printf("%X\n", C);
C = A >> 1;
printf("%X\n", C);
C = B >> 1;
printf("%X\n", C);
A = 100;
A = A >> 1; // A = A / 2
printf("%d\n", A);
A<<=1; // A *= 2;
printf("%d\n", A);
return 0;
}
RESULT:
12
FFFFFFDB
FFFFFFAC
65
0
FFFFFFC9
53
Shifts.......
FFFFFFA6
34
29
FFFFFFCD
50
100
Note: Setbit
#include <iostream>
using namespace std;
char* getBitPattern(unsigned int val, int fromIndex, int toIndex){
char* bp= new char[toIndex - fromIndex + 2];
int i, j;
for(i=toIndex,j=0;i>=fromIndex;i--,j++){
// slow
//if((1 << i) & val){
// bp[j] = '1';
//}
//else{
// bp[j] = '0';
//}
//fast
//bp[j] = ((1 << i) & val) ? '1':'0';
// faster
bp[j] = !!((1 << i) & val) + '0';
}
bp[j] = 0;
return bp;
}
int main(){
unsigned int v = 0x0000FF00;
char* bits = getBitPattern(v, 4, 11);
cout<<bits<<endl;
delete[] bits;
return 0;
}
The rerult of his problem is 11110000.
R0.5 NEED HELP - CCHECK
The problem in my ccheck is that I cannot choose the second option. Whatever I press, the 'X' still hold in the first one.
Here is my code:
CCheck::CCheck(bool Checked,const char* Format, const char* Text, int Row, int Col, int Width, bool IsRadio)
: CField(Row, Col, Width, 1), Label(Text, 0, 4, (Width-4)){
Label.frame((CFrame*)this);
_flag = (int) Checked;
_radio = (int) IsRadio;
strcpy(_format, Format);
_data = &_flag;
}
CCheck::CCheck(const CCheck& C): CField(C), Label(C.Label){
Label.frame((CFrame*)this);
_flag = C._flag;
_radio = C._radio;
strcpy(_format, C._format);
_data = &_flag;
}
void CCheck::draw(int fn){
bio_displayflag(_format, absRow(), absCol(), _flag);
Label.draw(fn);
}
int CCheck::edit(){
//eturns bio_flag()'s returned value.
return bio_flag(_format, absRow(), absCol(), &_flag, _radio);
}
bool CCheck::editable()const{
//Always return true;
return true;
}
void CCheck::set(const void* flag){
_flag = (int) flag;
}
bool CCheck::checked()const{
return true;
}
void CCheck::checked(bool val){
_flag = val;
}
Here is my code:
CCheck::CCheck(bool Checked,const char* Format, const char* Text, int Row, int Col, int Width, bool IsRadio)
: CField(Row, Col, Width, 1), Label(Text, 0, 4, (Width-4)){
Label.frame((CFrame*)this);
_flag = (int) Checked;
_radio = (int) IsRadio;
strcpy(_format, Format);
_data = &_flag;
}
CCheck::CCheck(const CCheck& C): CField(C), Label(C.Label){
Label.frame((CFrame*)this);
_flag = C._flag;
_radio = C._radio;
strcpy(_format, C._format);
_data = &_flag;
}
void CCheck::draw(int fn){
bio_displayflag(_format, absRow(), absCol(), _flag);
Label.draw(fn);
}
int CCheck::edit(){
//eturns bio_flag()'s returned value.
return bio_flag(_format, absRow(), absCol(), &_flag, _radio);
}
bool CCheck::editable()const{
//Always return true;
return true;
}
void CCheck::set(const void* flag){
_flag = (int) flag;
}
bool CCheck::checked()const{
return true;
}
void CCheck::checked(bool val){
_flag = val;
}
Subscribe to:
Posts (Atom)