Go Back   vb.org Archive > vBulletin 3 Discussion > vB3 General Discussions
FAQ Community Calendar Today's Posts Search

Reply
 
Thread Tools Display Modes
  #1  
Old 07-16-2005, 09:20 PM
akanevsky akanevsky is offline
 
Join Date: Apr 2005
Posts: 3,972
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default What are bitfields?

What are bitfields and how do they work? Esspecially, what does "&" operator do? Thanks.
Reply With Quote
  #2  
Old 07-16-2005, 09:36 PM
Adrian Schneider's Avatar
Adrian Schneider Adrian Schneider is offline
 
Join Date: Jul 2004
Posts: 2,528
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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
Reply With Quote
  #3  
Old 07-17-2005, 01:04 AM
deathemperor's Avatar
deathemperor deathemperor is offline
 
Join Date: Jul 2003
Location: HOL
Posts: 1,270
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

You may want to see this thread:

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

hope that help.
Reply With Quote
  #4  
Old 07-17-2005, 01:04 AM
akanevsky akanevsky is offline
 
Join Date: Apr 2005
Posts: 3,972
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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?
Reply With Quote
  #5  
Old 07-17-2005, 01:19 AM
Adrian Schneider's Avatar
Adrian Schneider Adrian Schneider is offline
 
Join Date: Jul 2004
Posts: 2,528
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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
Reply With Quote
  #6  
Old 07-17-2005, 01:27 AM
akanevsky akanevsky is offline
 
Join Date: Apr 2005
Posts: 3,972
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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?
Reply With Quote
  #7  
Old 07-17-2005, 01:34 AM
Adrian Schneider's Avatar
Adrian Schneider Adrian Schneider is offline
 
Join Date: Jul 2004
Posts: 2,528
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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)
Reply With Quote
  #8  
Old 07-17-2005, 01:36 AM
Andreas's Avatar
Andreas Andreas is offline
 
Join Date: Jan 2004
Location: Germany
Posts: 6,863
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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.
Reply With Quote
  #9  
Old 07-17-2005, 01:46 AM
akanevsky akanevsky is offline
 
Join Date: Apr 2005
Posts: 3,972
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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?)
Reply With Quote
  #10  
Old 07-17-2005, 01:54 AM
Andreas's Avatar
Andreas Andreas is offline
 
Join Date: Jan 2004
Location: Germany
Posts: 6,863
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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
Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 02:11 PM.


Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2024, vBulletin Solutions Inc.
X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.04751 seconds
  • Memory Usage 2,263KB
  • Queries Executed 13 (?)
More Information
Template Usage:
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (1)ad_showthread_beforeqr
  • (1)ad_showthread_firstpost
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (1)bbcode_php
  • (7)bbcode_quote
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)navbar
  • (3)navbar_link
  • (120)option
  • (1)pagenav
  • (1)pagenav_curpage
  • (1)pagenav_pagelink
  • (10)post_thanks_box
  • (10)post_thanks_button
  • (1)post_thanks_javascript
  • (1)post_thanks_navbar_search
  • (10)post_thanks_postbit_info
  • (10)postbit
  • (10)postbit_onlinestatus
  • (10)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available:
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files:
  • ./showthread.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/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_post_thanks.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_postinfo_query
  • fetch_postinfo
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • forumjump
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • post_thanks_function_post_thanks_off_start
  • post_thanks_function_post_thanks_off_end
  • post_thanks_function_fetch_thanks_start
  • post_thanks_function_fetch_thanks_end
  • post_thanks_function_thanked_already_start
  • post_thanks_function_thanked_already_end
  • fetch_musername
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • post_thanks_function_can_thank_this_post_start
  • pagenav_page
  • pagenav_complete
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • showthread_complete