   Bitwise Operation
  Ok my book has code to show a number in binary and I am only confused at one partif(u&t)   When does that equal true and when does it equal false. When it makes a change it equals true? And if it doesnt it equals false? I thought it just outputs a resulting binary sequence.void show_binary(unsigned int u){  int t;  for(t=128;t>0;t=t/2)   if(u&t) cout << "1 ";   else cout << "0 ";  cout << "\n";}
  The AND operation results in 1 if both of the operands are 1. Now, in bitwise operations, this is done for each bit.However, the condition in and if-statment uses the entire value, not just any single bit. If the value of the condition is not 0 (i.e. if any bit is 1), then the condition is true. If the value is 0 (i.e. all bits are 0), then the condition is false.For example, "if ( 5 & 4 ) ..." will take the true branch because 5 (101) & 4 (100) = 4 (100). Since a bit is set in the result, the condition is true.In your sample code, take a look at the values of t. Notice that they are 128, 64, 32, ... What are those values in binary? What is the loop doing?   ;  ok so I was right if basically any bit was changed it will return true   ;Quote:Original post by Supaflyfrankok so I was right if basically any bit was changed it will return trueNothing in this code is actually changing any bits in the first place. t has power of two, ie. it always has only one bit set. With &, t is used as a mask, so if this ONE bit in u is 1, the result is non-zero (true).But then, I don't quite see the point of using unsigned int, but only look at the last byte. Even if it's meant to avoid endian confusion, it would make more sense to make u an unsigned char in the first place.If you insist on thinking about changed bits, then this is true if this one particular bit is NOT changed in either number (ie. was 1 in both).   ;  Ok what you say makes some sense to me. And I understand that the other bits are masked by using zeros but what doesnt make sense to me if you have something like1   In Binary: 00000001254 In Binary: 11111110 and then boo ifs = (1&254)  if you print out ifs it displays 0 (false)65  In Binary: 01000001254 In Binary: 11111110 and then boo ifs = (1&65)if you print out ifs it displays 1  (true)127 In Binary: 01111111254 In Binary: 11111110 and then boo ifs = (127&254)if you print out ifs it displays 0 (false)reason i use bool ifs is to make it a true and false statement as if it was a if statement.For the 1 254 Im guessing because the other values have to be 0 you only are looking at the first bit which inturn is zero cause the first bit in 254 is 0For the 65 254 Because we are basically ignoring all the bits except the 7th and first because of a mask. And then the 7th bit in 65 and the 7th bit in 254 is 1 so I can see the result being 1 (true) but then the first bit in 65 is 1 and the first bit in 254 is 0 so the result of that AND statement would be 0. So then how come that entire sequence returns true?For the 127 and 254 everything is true except the last bit and everything is true except the first bit so how come the result of that AND is 0 (false)I understand from a bit by bit bases why you might get true and false but in a multiple bit sequence what determines if the entire sequence is true. I understand that AND really doesnt change the orginal. They say you use masks to only minipulate only a certain section but why cant you just modify it directly.  Making it so you can only see a certain group of numbers such as:10100101 number00000101 mask want to see the first 3 digits-------- Using And00000101  What did you truly accomplish?If you want to modify the second digit turning it on so in turn ouu could use OR like:1010010100000111--------10100111Basically getting the results you want.There is no bit or byte data type. the smallest data is bool which Im guessing is basically 1 bit but I dont get  why you need to mask anything for the fact you can just modify it directly.   ;  65 & 254 => 0100 0000127 & 254 => 0111 1110both eval to true because both are non-zero1 & 254 => 0000 0000is == 0, so it evals to falseA bool is often 8bits, unless you specify to the compile to pack several bools into one 8-bit segment.A char is also 8bits, but you can read all 8 of the bits, where a bool can only be treated as one bit.So, with, in your case above, an unsigned int, you have likely 32bits of data.with 32 bits you can store 32 bools.So, using a technique called bit-flags you can store lots of information in one integer.With bit-wise masks you can get each value.  This is how API's like directX and OpenGL store some of thier state information.It can also be seen in places like file attribute flags.ie.read_only 0001archive   0010system    0100hidden    1000so a file with flag = 1011 is read only, archive, and hidenso (flag & read_only) evaluates to true, since it is read only.if flag = 1000, then you can test (flag & hidden) and get true, but no other mask is true, so you know that it is not read only, system or archive.[Edited by - KulSeran on June 5, 2006 6:52:24 PM];Quote:Original post by SupaflyfrankMaking it so you can only see a certain group of numbers such as:10100101 number00000101 mask want to see the first 3 digits-------- Using And00000101  What did you truly accomplish?Nothing, because the mask would have to be 00000111 and not 00000101 (why would you "turn off" the second bit if you want to look at all three?)