View Full Version : What are bitfields?
akanevsky
07-16-2005, 09:20 PM
What are bitfields and how do they work? Esspecially, what does "&" operator do? Thanks.
Adrian Schneider
07-16-2005, 09:36 PM
They are fields that contain a binary number such as 37 (100101), which contains:
32 +
16
8
4 +
2
1 +
= 37
They are used for permissions in vB, as well as checkbox profile fields (probably others too). You basically work backwards to see if it contains a value or not.
Here:
http://ca.php.net/manual/en/language.operators.bitwise.php
deathemperor
07-17-2005, 01:04 AM
You may want to see this thread:
https://vborg.vbsupport.ru/showthread.php?t=76297
hope that help.
akanevsky
07-17-2005, 01:04 AM
Well, every programmer already knows this but for anyone new to the fun here's some quick clean code to determine if a number is odd or even.
function oddeven($x){
if($x & 1) return "odd";
else return "even";
}
I don't get this...
What does it mean - work backwars to see if it contains a value or not.
Say, 3 does not contain 1... Then how is it an odd number?
You may want to see this thread:
https://vborg.vbsupport.ru/showthread.php?t=76297
hope that help.
Not really... I still don't understand how one variable can contain another.. Maybe I need a guide on how binary works...
And what are bit variables? are they arrays? Or varibles that store data in a special form? Or just variables with binary-encoded data?
Adrian Schneider
07-17-2005, 01:19 AM
It basically goes through every 2^x where X is less than the # and if it can subtract the X, do so, then proceed - if it is < 0 then skip that subtraction and return false
So say for that function oddeven thing:
if the number is 20 (x = 20)
20 - 16 ( >= 0) x & 16 = true
4 - 8 ( < 0) x & 8 = false
4 - 4 ( >= 0) x & 4 = true
0 - 2 ( < 0) x & 2 = false
0 - 1 ( < 0) x & 1 = false
akanevsky
07-17-2005, 01:27 AM
I am confused...
"where X is less than the #" - what #?
"if it can subtract the X" - what is "it"?
Regarding the oddeven thing..
Please show me the process by which 5 & 1 determines that 5 is odd... Thanks.
32 +
16
8
4 +
2
1 +
= 37
Why does it skip of 2,8,16?
Adrian Schneider
07-17-2005, 01:34 AM
37-32 >= 0 so change the number to 5 (which is what is left over)
5 - 16 < 0, so ignore
5 - 8 < 0, so ignore
5 - 4 >= 0, so change the number to 1 (which is what is left over)
1 - 2 < 0, so ignore
1 - 1 >= 0, so change the number to 0
$num = 64;
for ($i=6; $i>0; $i--)
{
$result = pow(2, $i);
if ($num & $result)
{
echo 1;
}
else
{
echo 0;
}
}
outputs 100000 (same thing as what i just posted really)
Andreas
07-17-2005, 01:36 AM
A Bitfield is a Variable/Database Field where each Bit is being as a On-Off Setting.
This way you can store may settings in one integer.
Decimal 5 is Binaray 101 (1*2^0 + 0*2^1 + 1*2^2).
& is a binary AND operator, which can be used to check if a Bit is set: 101 & 001 = 001, eg. the Bit is set.
akanevsky
07-17-2005, 01:46 AM
Thanks, KirbyDE.
Could someone please explain how does this function work?
function oddeven($x){
if($x & 1) return "odd";
else return "even";
}
Decimal 5 is Binaray 101 (1*2^0 + 0*2^1 + 1*2^2).
Hmm...
2 in zero power multiplied by 1 is 1...
2 in first power multiplied by 0 is 0....
But 2 in second power multiplied by 1 is 5, not 1... (wtf?)
Andreas
07-17-2005, 01:54 AM
Could someone please explain how does this function work?
Well ... all Powers of 2 except 2^0 are even.
So if a number is odd, it must have Bit 0 (2^0=1) set, so this functions checks for this Bit being set.
But 2 in second power multiplied by 1 is 5, not 1... (wtf?)
1*2^0 = 1*1 = 1
0*2^1 = 0*2 = 0
1*2^2 = 1*4 = 4
In Total: 5
:)
deathemperor
07-17-2005, 04:30 AM
let me try to explain.
like the above example:
32
16
8
4
2
1
this is 65, in your database you have a field called permissionwhatever to store that number (65), and if a usergroup has that value 65 it means they have full permission, each bit represent for a permission. if it's 64, then the value you don't have must be 1, 60 means without 1 and 4 and it cannot be any other number. so that's the way it realizes you don't have what permission.
akanevsky
07-17-2005, 11:07 AM
Well ... all Powers of 2 except 2^0 are even.
So if a number is odd, it must have Bit 0 (2^0=1) set, so this functions checks for this Bit being set.
I don't understand the connection between 0 power and Bit 0? =//
1*2^0 = 1*1 = 1
0*2^1 = 0*2 = 0
1*2^2 = 1*4 = 4
In Total: 5
How do you convert decimals to binarys?
let me try to explain.
like the above example:
32
16
8
4
2
1
this is 65, in your database you have a field called permissionwhatever to store that number (65), and if a usergroup has that value 65 it means they have full permission, each bit represent for a permission. if it's 64, then the value you don't have must be 1, 60 means without 1 and 4 and it cannot be any other number. so that's the way it realizes you don't have what permission.
I understand this, thanks...
Marco van Herwaarden
07-17-2005, 11:13 AM
Also have a look at this post: https://vborg.vbsupport.ru/showpost.php?p=734362&postcount=4
Might help in understanding
akanevsky
07-17-2005, 12:04 PM
Okay, I already completely understand how the vB permissions work :)
What I do not understand is this:
Well ... all Powers of 2 except 2^0 are even.
So if a number is odd, it must have Bit 0 (2^0=1) set, so this functions checks for this Bit being set.
I don't understand the connection between 0 power and Bit 0? =//
Andreas
07-17-2005, 12:07 PM
Hmm, what exactly do you not understand?
2^0 = 1
2^1 = 2
2^2 = 4
2^3 = 8
2^4 = 16
...
As you can see, all numbers are even - except 1. So if you add them together, you will always get an even number if 1 is not included.
1 is 2^0, which means this bit must be set if a number is odd.
akanevsky
07-17-2005, 12:11 PM
Oh!!! Yeah, now I understand.
When a number is an even number, it consists of such sub-numbers, as 2,4,8,16, but not 1. And when a number is odd, it has 1 included, so 3 & 1 works :) This is so cool...
Thanks everyone.
vBulletin® v3.8.12 by vBS, Copyright ©2000-2025, vBulletin Solutions Inc.