Reputation: 11
Can someone explain this in simpler terms?
The binary representation of 170 is 0000 0000 1010 1010
. The binary representation of 75 is 0000 0000 0100 1011
. Performing the bitwise AND operation on these two values produces the binary result 0000 0000 0000 1010
, which is decimal 10.
0000 0000 1010 1010
0000 0000 0100 1011
-------------------
0000 0000 0000 1010
This will make will click for me once I know what is being done. I have a basic understanding of binaries and know a few off the top of my head... like 1 represented in binary would be 00000001
and 2 would be 00000010
and 3 would be 00000011
and 4 would be 00000100
and 5 would be 00000101
and 6 would be 00000110
. So I understand what is going on when you got up a digit each time.
I also understand what is going on when this sql developer is subtracting, but not something is missing when she uses t-sql code to find her answers.... in regards to what is stated in this link.
http://sqlfool.com/2009/02/bitwise-operations/
Upvotes: 0
Views: 788
Reputation: 180777
Look at the individual binary digits in your example as columns. If there is a 1 in both input rows of a particular column, the output is 1 for that column. Otherwise it is 0.
The AND operator can be used to "mask" values. So if you just want the first four low-order bits of a number, you can AND it with 15, like this:
0010 1101 1110 1100
0000 0000 0000 1111
-------------------
0000 0000 0000 1100 <-- the value of the first four bits in the top number
That's what is happening in the SQL example you linked.
freq_interval is one or more of the following:
1 = Sunday
2 = Monday
4 = Tuesday
8 = Wednesday
16 = Thursday
32 = Friday
64 = Saturday
Corresponds to the bit masks:
0000 0001 = Sunday
0000 0010 = Monday
0000 0100 = Tuesday
0000 1000 = Wednesday
0001 0000 = Thursday
0010 0000 = Friday
0100 0000 = Saturday
Upvotes: 3