Nolongerused3921 Posted December 26, 2006 Share Posted December 26, 2006 Well its official: I'm stumped... I can't seem to get my bitwise function to work very well at all... I'm not quite sure whats wrong with it exactly, but its not returning the values you would think it would.The function:[code]<?phpfunction perm_check($permissions, $module_permission, $permission_prefix, $action = 1) { $temp_breakdown = explode(";",$permissions); $temp_count = count($temp_breakdown); for ($i = 0; $i != $temp_count; $i++){ if (is_array($temp_breakdown)) $tmp_breakdown = $temp_breakdown[$i]; else $tmp_breakdown = $temp_breakdown; if(strstr($tmp_breakdown,$permission_prefix)){ $permission = explode(':',$tmp_breakdown); $permission = bin2hex($permission[1]); $module_permission = $module_permission; break; } } switch ($action) { case 1: if ($permission & $module_permission) $val = 1; else $val = 0; break; case 2: $permission = $permission | $module_permission; $val = $permission; break; } return $val;}[/code]Should probably explain this... Basically the first 15 lines or so are just to split the string ("module_name:0x########;module_name:blah;etc.") up, then gather the correct permission... That part works fine - it even converts the string into real hex (Which stays as a string otherwise).However, the problem is with the switch and the whole bitwise portion... Whats wrong with it?Heres what I'm using to test it:[code]<?php$groups_permissions = "article:0x00000000;";$view_thread = 16;$view_post = 2;$perms = perm_check($groups_permissions,$view_post,"article",2);//print $perms;if ($perms & $view_thread) { print "Success!";}[/code]Now... You'd think this wouldn't work, right? Nope.... For whatever reason, the if activates.What am I doing wrong here? Link to comment https://forums.phpfreaks.com/topic/31866-bitwise-operator-not-working-correctly-with-hex/ Share on other sites More sharing options...
Vikas Jayna Posted December 26, 2006 Share Posted December 26, 2006 Could you post the expected output and the output obtained from this code Link to comment https://forums.phpfreaks.com/topic/31866-bitwise-operator-not-working-correctly-with-hex/#findComment-147960 Share on other sites More sharing options...
Nolongerused3921 Posted December 27, 2006 Author Share Posted December 27, 2006 Err.... "Success" is what I get from the source, and it shouldn't return success, seeing as $perms & $view_thread doesn't exist. :($perms = perm_check($groups_permissions,$view_post,"article",2)Basically this does "0x00000000 | $view_post", and assigns the result to $perms.Now as converted to hex (0x02) or left as decimal, this doesn't work how its supposed to. Link to comment https://forums.phpfreaks.com/topic/31866-bitwise-operator-not-working-correctly-with-hex/#findComment-148140 Share on other sites More sharing options...
Nolongerused3921 Posted December 27, 2006 Author Share Posted December 27, 2006 Okay well I went through and made sure everything is in hex... I'm getting weird results though....New function:[code=php:0]function perm_check($permissions, $module_permission, $permission_prefix, $action = 1) { $temp_breakdown = explode(";",$permissions); $temp_count = count($temp_breakdown); for ($i = 0; $i != $temp_count; $i++){ if (is_array($temp_breakdown)) $tmp_breakdown = $temp_breakdown[$i]; else $tmp_breakdown = $temp_breakdown; if(strstr($tmp_breakdown,$permission_prefix)){ $permission = explode(':',$tmp_breakdown); $permission = $permission[1]; $module_permission = dechex($module_permission); break; } else { $permission = $permissions; $module_permission = dechex($module_permission); } } switch ($action) { case 1: print "<br>Permission: ".$permission."<br>"; print "Module_permission: ".$module_permission; if ($permission & $module_permission) $val = 1; else $val = 0; break; case 2: $val = $permission | $module_permission; $val = dechex($val); break; } return $val;}[/code]New test:[code=php:0]$groups_permissions = "article:0x00000000;";$view_thread = 16;$view_post = 2;$perms = perm_check($groups_permissions,$view_post,"article",2);$perms = perm_check($perms,$view_thread,"article",2);print $perms;if (perm_check($perms,$view_thread,"article",1)) { print "<br>Success!";}[/code]For some reason my result is:[quote]1ePermission: 1eModule_permission: 10Success![/quote]...What exactly is 1e doing there? Shouldn't it be 12 (16 (10 in hex) + 2 (02 in hex))? Am I doing something wrong, and if so... What?Edit: What the ...I took out $val = dechex($val) in the function, and now I get:[quote]3x00000000Permission: 3x00000000Module_permission: 10Success! [/quote]I'm 90% sure this is actually working how its supposed to (At least the check is)... But why is it returning 3x00000000? Link to comment https://forums.phpfreaks.com/topic/31866-bitwise-operator-not-working-correctly-with-hex/#findComment-148154 Share on other sites More sharing options...
kenrbnsn Posted December 27, 2006 Share Posted December 27, 2006 It's always helpful to put echo statements in your code when trying to debug problems like this.The bin2hex() function wasn't doing what you thought it was doing. I replaced it with the [url=http://www.php.net/sscanf]sscanf()[/url] function.Here's my take on your code, with the debug statements left in:[code]<?phpfunction perm_check($permissions, $module_permission, $permission_prefix, $action = 1) { $temp_breakdown = explode(";",$permissions); $temp_count = count($temp_breakdown); for ($i = 0; $i < $temp_count; $i++){ $tmp_breakdown = $temp_breakdown[$i]; if(strstr($tmp_breakdown,$permission_prefix)){ $permission = explode(':',$tmp_breakdown); list($permissionx) = sscanf($permission[1],"%X"); break; } } echo '<pre>' . print_r($permission,true) . '</pre>'; echo sprintf("%08X<br>------<br>",$permissionx); switch ($action) { case 1: if ($permissionx & $module_permission) $val = 1; else $val = 0; break; case 2: $permissiony = $permissionx | $module_permission; $val = $permissiony; break; } return $val;}$groups_permissions = "article:0x00000000;";$view_thread = 16;$view_post = 2;$perms = perm_check($groups_permissions,$view_post,"article",2);$chk_perms = $perms & $view_thread;echo sprintf("%08X<br>%08X<br>%08X<br>",$perms,$view_thread,$chk_perms);if ($chk_perms) { print "Success!";}?>[/code]Ken Link to comment https://forums.phpfreaks.com/topic/31866-bitwise-operator-not-working-correctly-with-hex/#findComment-148156 Share on other sites More sharing options...
Nolongerused3921 Posted December 27, 2006 Author Share Posted December 27, 2006 Actually I took the print(s) from the code to clean it up some for posting here.And I don't understand what you're saying... Are you saying the 0x isn't needed when you use dechex() on a string? So I should remove the 0x before parsing and add it again after parsing...? Link to comment https://forums.phpfreaks.com/topic/31866-bitwise-operator-not-working-correctly-with-hex/#findComment-148199 Share on other sites More sharing options...
Nolongerused3921 Posted December 27, 2006 Author Share Posted December 27, 2006 I took out the 0x with a preg_match + sscanf, and at least its printing nomral numbers now... But somehow it "loses" a permission or something similar... Its very odd..Code:[code=php:0]function perm_check($permissions, $module_permission, $permission_prefix, $action = 1) { $temp_breakdown = explode(";",$permissions); $temp_count = count($temp_breakdown); for ($i = 0; $i != $temp_count; $i++){ if (is_array($temp_breakdown)) $tmp_breakdown = $temp_breakdown[$i]; else $tmp_breakdown = $temp_breakdown; if(strstr($tmp_breakdown,$permission_prefix)){ $permission = explode(':',$tmp_breakdown); $permission = $permission[1]; //sscanf($permission,"%X",$permission); //$module_permission = dechex($module_permission); print "In here!<br>"; break; } else { $permission = $permissions; print "Mod_perm: ".$module_permission."<br>"; $module_permission = dechex($module_permission); } } if (preg_match("/0x/",$permission)) { sscanf($permission,"%X",$permission); print "Removing 0x: $permission<br>"; } print "Permission above switch: ".$permission."<br>"; print "Module_permission above switch: ".$module_permission."<br>"; switch ($action) { case 1: print "<br>Permission: ".$permission."<br>"; print "Module_permission: ".$module_permission; if ($permission & $module_permission) return 1; else return 0; break; case 2: $val = $permission | $module_permission; return($val); break; }} $groups_permissions = "article:0x00000000;"; $view_thread = 16; $view_post = 2; print "First permission check:"; $perms = perm_check($groups_permissions,$view_post,"article",2); print "perms in groups: $perms <br>"; print "<br><br>Second permission check:<br>"; $perms = perm_check($perms,$view_thread,"article",2); print "perms in groups: $perms <br>"; print "<br><br>Third permission check (If):<br>"; if (perm_check($perms,$view_thread,"article",1)) { print "<br>Success!"; }[/code]Output:[quote]First permission check:In here!Removing 0x: 0Permission above switch: 0Module_permission above switch: 2perms in groups: 2 Second permission check:Mod_perm: 16Permission above switch: 2Module_permission above switch: 10perms in groups: 10 Third permission check (If):Mod_perm: 16Permission above switch: 10Module_permission above switch: 10Permission: 10Module_permission: 10Success![/quote] Link to comment https://forums.phpfreaks.com/topic/31866-bitwise-operator-not-working-correctly-with-hex/#findComment-148217 Share on other sites More sharing options...
Nolongerused3921 Posted December 27, 2006 Author Share Posted December 27, 2006 Anyone... Any help is appreciated Link to comment https://forums.phpfreaks.com/topic/31866-bitwise-operator-not-working-correctly-with-hex/#findComment-148344 Share on other sites More sharing options...
kenrbnsn Posted December 27, 2006 Share Posted December 27, 2006 The code I posted works.Ken Link to comment https://forums.phpfreaks.com/topic/31866-bitwise-operator-not-working-correctly-with-hex/#findComment-148393 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.