bibby Posted July 13, 2006 Share Posted July 13, 2006 I'm support, not dev just so that's clear.At the workplace, there's a system of flags for users stored as a decimal integer, but represent bits.0001 means that the first flag is raised therefore the DB stores 1 (person A)0100 means the same for the third, and stores 4 (person B)0111 , first three flags raised , so I get 7. (person C) This I understand, and it's not the problem. We have 50 or so flags and it makes things pretty easy.What I don't get is the query we use to find out who has what flag raised..(example structure: id tinytext, bit bigint)If persons A,B, and C were in the database with the values 1,4,7 above, and I wanted to know who among them has the third flag raised, we queryselect id from persons where (bit & 4)=4;It returns B, CTo get the first flag,select id from persons where (bit & 1)=1;It returns A, C It works like a charm, but I can't for the life of me wrap my head around how the query statement finds what I'm looking for. Can anyone explain to me what is going on in "where(bit & 4)=4" ?Thanks in advance Link to comment https://forums.phpfreaks.com/topic/14439-where-bit-44/ Share on other sites More sharing options...
effigy Posted July 13, 2006 Share Posted July 13, 2006 See if [url=http://en.wikipedia.org/wiki/Bitwise_operation#AND]this[/url] helps. The & matches up all the "on" bits of both operands. Therefore "bit & 4" reads as "where the four bit in 'bit' is on."[tt] 100& 100------ 100[/tt] Link to comment https://forums.phpfreaks.com/topic/14439-where-bit-44/#findComment-57120 Share on other sites More sharing options...
bibby Posted July 13, 2006 Author Share Posted July 13, 2006 Very cool. It also explains why '&&' is used in most php IF statements (returns T/F).That helped a lot. Thanks! Link to comment https://forums.phpfreaks.com/topic/14439-where-bit-44/#findComment-57446 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.