![]() |
|
|
|
| ||||||
|
Welcome to the The ProgrammersTalk Community forums. You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today! If you have any problems with the registration process or your account login, please contact contact us. |
| Tags: bitwise, help, logical, operator, programming, tutorial |
![]() |
![]() | | LinkBack | Thread Tools | Display Modes | ![]() |
| |
| |||
| Bitwise = manipulation (of numbers) Logical = boolean test(s) of expression(s) For example: Code: x = 4 | 1; //bitwise - results in 5 (0100 OR 0001 = 0101, which is 5)
if ((x == 4) || (x == 1)) { /* code */ } //logical - if x is 4 then the code is executed. if x is not 4, but x is 1 the code is executed. if x is neither 4 nor 1, the code is not executed.
if (((x & 4) != 0) || ((x & 1) != 0)) { /* code */ } //logical using bitwise operators - the bitwise AND returns a value of 4 if the bit at the 4s place (binary digit place) or 0 if the bit is not set. Next, you test that returned value to see if it is not 0. If it is not 0, then the code is executed. If it is 0, then the same process is repeated, but with "x & 1" instead of "x & 4". Code: int INTENSITY = 0x8, RED = 0x4, GREEN = 0x2, BLUE = 0x1; int MAGENTA = RED | BLUE; int color = 9; print_color(color); color ^= RED; //if ((color & RED) == 0) color |= RED; else color -= RED; print_color(color); 9 13 Or perhaps like this: INTENSE BLUE INTENSE MAGENTA Either way, that shows how bitwise operators can be used to manipulate values. In fact, there is a technique using the bitwise XOR operator that sets a value to 0 quickly. The reason why it exists is due to Assembly programming, where MOV instructions can be somewhat costly at times, especially if you just wanted to change a value to 0. Here it is (the ^ character is the XOR operator: Code: variable_n ^= variable_n; Code: variable_n = variable_n ^ variable_n; With regards to this interesting trick, consider that variable_n = 14 (1110 in binary). Use XOR in binary on a single column at a time, and remember that 1 XOR 1 = 0 and 0 XOR 0 = 0: Code: 1110
XOR 1110
--------
0000 Code: if (x == 1 & x == 2) // may act like "if (x == (1 & x) == 2)" or "if ((x == 1) & (x == 2))" depending on operator precedence in the language that you are using Code: if (x == 1 && x == 2) // again, may act differently depending on operator precedence |
| The Following 2 Users Say Thank You to rpgfan3233 For This Useful Post: | ||
HelloWorld (07-15-2007), TeraTask (07-15-2007) | ||
| ||||
| Nice write up rpgfan3233. Let me expound on these 2 codes bases (adjusted for apparent intent and for additional complexity): CONDITIONAL #1 Code: if ((x == 1) & (x == 2) & (x == 3)) CONDITIONAL #2 Code: if ((x == 1) && (x == 2) && (x == 3)) CONDITIONAL #1 (assuming LTR processing) REPLACE VALUES: if (( 3 == 1) & (3 == 2) & (3 == 3)) PERFOM EVALUATION: if (FALSE & (3 == 2) & (3 == 3)) PERFOM EVALUATION: if (FALSE & FALSE & (3 == 3)) PERFOM EVALUATION: if (FALSE & FALSE & TRUE) PERFOM EVALUATION: if (FALSE & TRUE) PERFOM EVALUATION: if (FALSE) CONDITIONAL #2 (assuming LTR processing) REPLACE VALUES: if (( 3 == 1) && (3 == 2) && (3 == 3)) PERFORM EVALUATION: if (FALSE && (3 == 2) && (3 == 3)) Why did Conditional #2 stop processing first? One of the conditions was false, so comparison was no longer necessary. Conditional #1 is like a long, binary mathematical statment. Conditional #2 is a logical comparison of individual comparisons. There's a slight difference there. I have an article that should be out in about 4 hours (hopefully less), which will discuss a situation when using & can be helpful. |
| The Following User Says Thank You to TeraTask For This Useful Post: | ||
HelloWorld (07-15-2007) | ||
| ||||
| Quote:
rpgfan, yes, I do remember that there's XOR operation, but I never used it. Why would you want to use that operation? Can you give me some examples? What's the difference between '&' bitwise operator and '|' ??? I never used them all, like... at all.. never... so please guide me so that at least I can use them efficiently on a certain occassion ![]() |
| |||
| The bitwise operators act the same way as their logical operator counterparts, except the bitwise operators work with each bit of a numeric value individually whereas a logical operator is used to work with true/false values. XOR is good for a couple of things:
Uses for AND '&':
Code: //goal is to make x = -4 int x = 4; print (x * -1); //-1 print ((~x) + 1); //-1, can also be print(++(~x)); I think Last edited by rpgfan3233 : 07-15-2007 at 08:22 PM. |
| The Following 2 Users Say Thank You to rpgfan3233 For This Useful Post: | ||
HelloWorld (07-15-2007), lwinaung (07-17-2008) | ||
| ||||
| I tried the following code: PHP Code: Code: a: 1 b: 2 a: 3 b: 2 a: 3 b: 1 ![]() |
| ||||
| Nevermind pal, I got it right, I tried it again by adding the last line... Code: a ^= b;
System.out.println("a: " + a);
System.out.println("b: " + b); ![]() Do you think this is just exaclty the same if I use temp variable? Is it ok to use this swapping technique habitually...? Last edited by HelloWorld : 07-15-2007 at 09:09 PM. |
| |||
| Quote:
a = 1, b = 2 In binary, a = 0001, b = 0010. Now when we use XOR the first time: Code: a = a XOR b (a ^= b)
0001 (a)
XOR 0010 (b)
--------
0011 Now, a = 3 but b = 2 still because we haven't actually changed b yet. Next XOR: Code: b = b XOR a (b ^= a)
0010 (b)
XOR 0011 (a)
--------
0001 Now, a = 3 and b = 1. We've already got half of it done! Final XOR: Code: a = a XOR b (a ^= b)
0011 (a)
XOR 0001 (b)
--------
0010 The swap is done. a = 2 now, and b = 1 now. |
| The Following User Says Thank You to rpgfan3233 For This Useful Post: | ||
HelloWorld (07-15-2007) | ||
| ||||
| Trillion thanx a lot rpgfan, you're great! **can't give more than one thanx** ![]() I learned a lot from your examples ![]() BTW, you memorized all of the bit codes? 0001 or 1000 or 1100, etc? Do you think it's important to know at least some of them those are basics? also, is it a good habit to use this swapping system every time? P.S sorry I've too many questions, that's cuz I'm really amazed to bit stuff now... lol... |
| |||
| Bit codes are easy. You just have to know that it is all binary. For example, 16 = 2^4 (2 to the 4th power, not 2 XOR 4, in this case): 1*(2^4) + 0*(2^3) + 0*(2^2) + 0*(2^1) + 0*(2^0) = 1*16 + 0*8 + 0*4 + 0*2 + 0*1 = 16 + 0 + 0 + 0 + 0 = 16 You see the x*(2^n) + y*(2^p) + ...? x and y are sometimes referred to as "coefficients". These coefficients are what makes up a binary number. In other words, you just write down the coefficients to make the number binary: 10000 = 16. I suggest you check out Kingsley-Hughes.Com | Quick Binary Tutorial for more information. If that doesn't work for you, you can always just look for a different binary tutorial. Edit: These are the ones I know by heart and can recite: 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536 That is the powers of 2 from 2^0 through 2^16. I also know 4294967296 (2^32). I should really know 2147483648 (2^31) as well since the upper limit of an int in Java (or signed long int in C/C++) is 2147483647 (2^31 - 1). The upper limit of an unsigned long int in C/C++ is 4294967295 (2^32 - 1). Last edited by rpgfan3233 : 07-15-2007 at 10:20 PM. |
| The Following User Says Thank You to rpgfan3233 For This Useful Post: | ||
HelloWorld (07-15-2007) | ||
![]() |
| Thread Tools | |
| Display Modes | |
| |