Operator Or
-
Hi, I'd like write a function doing sth like that.
if(a == 5)
val = val | 1000if( b == 20)
val= val | 100in the end if i have the both condictions corrects i'd like to have 1100 as result on val. but i always do 1000. which operator should i use to have the 1100 as resut.
"The Ultimate Limit Is Only Your Imagination."
-
Hi, I'd like write a function doing sth like that.
if(a == 5)
val = val | 1000if( b == 20)
val= val | 100in the end if i have the both condictions corrects i'd like to have 1100 as result on val. but i always do 1000. which operator should i use to have the 1100 as resut.
"The Ultimate Limit Is Only Your Imagination."
why are you using bitwise OR, if what you really want is addition ?
-
Hi, I'd like write a function doing sth like that.
if(a == 5)
val = val | 1000if( b == 20)
val= val | 100in the end if i have the both condictions corrects i'd like to have 1100 as result on val. but i always do 1000. which operator should i use to have the 1100 as resut.
"The Ultimate Limit Is Only Your Imagination."
Being slightly cynical addition sounds like the operator you neeed... With your statements val is going to end up containing 1004 as there's a large overlap in the bits set between the bit pattern for 1000 (1111101000b) and the bit pattern for 100 (1100100b). If you're trying to fiddle individual bits (i.e. turn bits with OR 1, turn them off with AND 0) then perhaps encode your numbers as powers of two and/or use hex:
if( a == 5 )
val |= 0x1000;if( b == 20 )
val |= 0x100;Cheers, Ash PS: Another way of setting and resetting individual bits is to use bit fields. Perhaps they might be more in tune with what you want?
-
Being slightly cynical addition sounds like the operator you neeed... With your statements val is going to end up containing 1004 as there's a large overlap in the bits set between the bit pattern for 1000 (1111101000b) and the bit pattern for 100 (1100100b). If you're trying to fiddle individual bits (i.e. turn bits with OR 1, turn them off with AND 0) then perhaps encode your numbers as powers of two and/or use hex:
if( a == 5 )
val |= 0x1000;if( b == 20 )
val |= 0x100;Cheers, Ash PS: Another way of setting and resetting individual bits is to use bit fields. Perhaps they might be more in tune with what you want?
int blo=0;
for(int i=0; i< New.GetLength(); i++ )
{
if(isalpha(New.GetAt(i)))
{
blo |=0x1000;
if(isupper(New.GetAt(i))) //Upper case KeyStroke
blo |= 0x100;
else
blo |=0x10; //Lower Case KeyStroke
}
}if i try it with Blood what will give me is 4352 or what i need is 1100 (1000 for being an alpha and 100 for being uppercase¨.
"The Ultimate Limit Is Only Your Imagination."
-
Being slightly cynical addition sounds like the operator you neeed... With your statements val is going to end up containing 1004 as there's a large overlap in the bits set between the bit pattern for 1000 (1111101000b) and the bit pattern for 100 (1100100b). If you're trying to fiddle individual bits (i.e. turn bits with OR 1, turn them off with AND 0) then perhaps encode your numbers as powers of two and/or use hex:
if( a == 5 )
val |= 0x1000;if( b == 20 )
val |= 0x100;Cheers, Ash PS: Another way of setting and resetting individual bits is to use bit fields. Perhaps they might be more in tune with what you want?
may be i need some var type instead of the int .... what kind of var should i use to hold the 11101110 (byte
"The Ultimate Limit Is Only Your Imagination."
-
int blo=0;
for(int i=0; i< New.GetLength(); i++ )
{
if(isalpha(New.GetAt(i)))
{
blo |=0x1000;
if(isupper(New.GetAt(i))) //Upper case KeyStroke
blo |= 0x100;
else
blo |=0x10; //Lower Case KeyStroke
}
}if i try it with Blood what will give me is 4352 or what i need is 1100 (1000 for being an alpha and 100 for being uppercase¨.
"The Ultimate Limit Is Only Your Imagination."
Blood_HaZaRd wrote:
if i try it with Blood what will give me is 4352 or what i need is 1100...
You are confusing base-10 with base-16.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Man who follows car will be exhausted." - Confucius
-
int blo=0;
for(int i=0; i< New.GetLength(); i++ )
{
if(isalpha(New.GetAt(i)))
{
blo |=0x1000;
if(isupper(New.GetAt(i))) //Upper case KeyStroke
blo |= 0x100;
else
blo |=0x10; //Lower Case KeyStroke
}
}if i try it with Blood what will give me is 4352 or what i need is 1100 (1000 for being an alpha and 100 for being uppercase¨.
"The Ultimate Limit Is Only Your Imagination."
-
So are you trying to get a count of the number of alpha characters, the number of uppercase and the number of lowercase and encode them into a set of decimal digits?
i dont need to count them but what i need is if the user typed all uppercases the res will be 1000. if he typed only locases it ll be 100 if he typed both it will 1100. i need sth like this coz i have more checking to do for numbers, dots. so let's say if he typed one upper, 5 low, 3 digits and zero dot the result ll be 1101
"The Ultimate Limit Is Only Your Imagination."
-
i dont need to count them but what i need is if the user typed all uppercases the res will be 1000. if he typed only locases it ll be 100 if he typed both it will 1100. i need sth like this coz i have more checking to do for numbers, dots. so let's say if he typed one upper, 5 low, 3 digits and zero dot the result ll be 1101
"The Ultimate Limit Is Only Your Imagination."
// All powers of 2 are good
const DWORD TypeUpper = 0x00001000;
const DWORD TypeLower = 0x00000100;
const DWORD TypeDot = 0x00000001;
...DWORD characterTypes = 0;
loop
{
if (isupper(...))
characterTypes |= TypeUpper;
else if (isdot(...))
characterTypes |= TypeDot;
else if (...)
...
}if (characterTypes == TypeUpper)
cout << "contains only uppercase letters\n";
else if (characterTypes & TypeUpper)
cout << "contains at least one uppercase letter\n";if (characterTypes == TypeLower)
cout << "contains only lowercase letters\n";
else if (characterTypes & TypeLower)
cout << "contains at least one lowercase letter\n";
...cout.flush();
-
i dont need to count them but what i need is if the user typed all uppercases the res will be 1000. if he typed only locases it ll be 100 if he typed both it will 1100. i need sth like this coz i have more checking to do for numbers, dots. so let's say if he typed one upper, 5 low, 3 digits and zero dot the result ll be 1101
"The Ultimate Limit Is Only Your Imagination."
In that case I'd use a union of bit fields and an integer. Then you can set bits to your hearts content and read it as a number, something like:
union
{
struct
{
unsigned has_upper_case_ : 1;
unsigned has_lower_case_ : 1;
unsigned has_a_digit_ : 1;
unsigned has_dot_ : 1;
} bits_;unsigned as\_number\_;
}
text_flags;Then you can use it like:
text_flags.as_number_ = 0;
text_flags.bits_.has_a_digit_ = 1;
std::cout << text_flags.as_number_ << std::endl;
That'll print 2 on any compiler that arranges the bit fields from lowest significance to highest. I can't remember if the standard says anything about the ordering of bits - it's not the sort of thing I play with that often - so check that the ordering is what you'd expect. IF you do use something like this you can add a constructor and members to your union. That might be an idea to save fannying about remembering to zero the thing and you could have set and reset methods. Cheers, Ash PS: There's a good description of bit fields in "The C++ Programming Language" 3rd edition if you get stuck and want help from the great man himself. I think he uses the page tables of a RISC processor as somewhere you could use bit fields like this. PPS: Corrected a typo in the code, bits in the declaration of the union didn't have the trailing underscore I gave it in use.
modified on Thursday, June 10, 2010 4:02 AM
-
In that case I'd use a union of bit fields and an integer. Then you can set bits to your hearts content and read it as a number, something like:
union
{
struct
{
unsigned has_upper_case_ : 1;
unsigned has_lower_case_ : 1;
unsigned has_a_digit_ : 1;
unsigned has_dot_ : 1;
} bits_;unsigned as\_number\_;
}
text_flags;Then you can use it like:
text_flags.as_number_ = 0;
text_flags.bits_.has_a_digit_ = 1;
std::cout << text_flags.as_number_ << std::endl;
That'll print 2 on any compiler that arranges the bit fields from lowest significance to highest. I can't remember if the standard says anything about the ordering of bits - it's not the sort of thing I play with that often - so check that the ordering is what you'd expect. IF you do use something like this you can add a constructor and members to your union. That might be an idea to save fannying about remembering to zero the thing and you could have set and reset methods. Cheers, Ash PS: There's a good description of bit fields in "The C++ Programming Language" 3rd edition if you get stuck and want help from the great man himself. I think he uses the page tables of a RISC processor as somewhere you could use bit fields like this. PPS: Corrected a typo in the code, bits in the declaration of the union didn't have the trailing underscore I gave it in use.
modified on Thursday, June 10, 2010 4:02 AM
Thank you soo much the first one runs great the secondm francly i didn't undestand it so i ll try it later :laugh: anyway thank you again for your great help :thumbsup:
"The Ultimate Limit Is Only Your Imagination."