vb.org Archive

vb.org Archive (https://vborg.vbsupport.ru/index.php)
-   vB3 General Discussions (https://vborg.vbsupport.ru/forumdisplay.php?f=111)
-   -   What are bitfields? (https://vborg.vbsupport.ru/showthread.php?t=92342)

akanevsky 07-16-2005 08:20 PM

What are bitfields?
 
What are bitfields and how do they work? Esspecially, what does "&" operator do? Thanks.

Adrian Schneider 07-16-2005 08: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...rs.bitwise.php

deathemperor 07-17-2005 12:04 AM

You may want to see this thread:

https://vborg.vbsupport.ru/showthread.php?t=76297

hope that help.

akanevsky 07-17-2005 12:04 AM

Quote:

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?

Quote:

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 12: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 12: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.

Quote:

32 +
16
8
4 +
2
1 +
= 37
Why does it skip of 2,8,16?

Adrian Schneider 07-17-2005 12: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

PHP Code:

$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 12: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 12:46 AM

Thanks, KirbyDE.

Could someone please explain how does this function work?

Quote:

function oddeven($x){
if($x & 1) return "odd";
else return "even";
}
Quote:

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 12:54 AM

Quote:

Originally Posted by Dark Visor
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.

Quote:

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
:)


All times are GMT. The time now is 08:28 PM.

Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2025, vBulletin Solutions Inc.

X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.03348 seconds
  • Memory Usage 1,741KB
  • Queries Executed 10 (?)
More Information
Template Usage:
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (1)bbcode_php_printable
  • (7)bbcode_quote_printable
  • (1)footer
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (6)option
  • (1)pagenav
  • (1)pagenav_curpage
  • (1)pagenav_pagelink
  • (1)post_thanks_navbar_search
  • (1)printthread
  • (10)printthreadbit
  • (1)spacer_close
  • (1)spacer_open 

Phrase Groups Available:
  • global
  • postbit
  • showthread
Included Files:
  • ./printthread.php
  • ./global.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/class_hook.php
  • ./includes/modsystem_functions.php
  • ./includes/class_bbcode_alt.php
  • ./includes/class_bbcode.php
  • ./includes/functions_bigthree.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • printthread_start
  • pagenav_page
  • pagenav_complete
  • bbcode_fetch_tags
  • bbcode_create
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • printthread_post
  • printthread_complete