Bitwise Operators
-
Just wanted to ask if anyone has any good online reading info on bitwise operators. I am kind of new to c++ and programming all together and these bitwise operators are causing some serious "illegal operations" :eek: to occur in my brain. Specifically, I am having trouble understanding an example (from Ivor Hortin's Beginning Visual C++ 6, Pg. 1074) which divides a number by eight and calculates the remainder using bitwise operators: 29 = (3x8) +5 29 = 0000 0000 0001 1101 7 = 0000 0000 0000 0111 ========================= 0000 000 0000 0101 = rem 5 My question is how would one figure out that they needed to use the binary of the number 7 to divide by eight? I hope this makes sense. Thanks
-
Just wanted to ask if anyone has any good online reading info on bitwise operators. I am kind of new to c++ and programming all together and these bitwise operators are causing some serious "illegal operations" :eek: to occur in my brain. Specifically, I am having trouble understanding an example (from Ivor Hortin's Beginning Visual C++ 6, Pg. 1074) which divides a number by eight and calculates the remainder using bitwise operators: 29 = (3x8) +5 29 = 0000 0000 0001 1101 7 = 0000 0000 0000 0111 ========================= 0000 000 0000 0101 = rem 5 My question is how would one figure out that they needed to use the binary of the number 7 to divide by eight? I hope this makes sense. Thanks
If you shift a binary number to the left by 1 bit, you are effectively diving that number by two (2^1 = 2). 0000 0000 0001 1101 = 29 Shift left by three bits (2^3 = 8, so we're dividing by 8): 0000 0000 0000 0011 -> 101 (101 are the three bits shifted out. I suppose this is the remainder. 101 binary also equals 5 decimal.) 0000 0000 0000 0011 = 3 0000 0000 0000 0101 = 5 In regular division: 29 / 8 = 3 rem 5, so the result is correct. I'm not sure where the 7 came from, though. These are just my observations. I don't know if this is mathematically correct. Perhaps one of our resident math majors can prove this for you. :) [EDIT] Sorry, I don't have any good online resources for reading about bitwise operators. I think the best thing you can do is to study the binary number system. [/EDIT] Jon Sagara What about :bob:? Sonork ID: 100.9999 jonsagara