Jump to content

decbin() - Is there an upper limit?


Go to solution Solved by kicken,

Recommended Posts

From what I have read decbin has an upper limit of 2 to the power of 52, so what is going wrong with this code?

function teams($number){
  		$number = decbin($number);  //Change the number to Binary
   		if($number % 10) {echo " <font color=green>ARENA</font> ";}  //1
  		if($number / 10 % 10) {echo " <font color=maroon>FLEET</font> ";}  //2
  		if($number / 100 % 10) {echo " <font color=yellow>FLEET RESERVE</font>";}  //4
  		if($number / 1000 % 10) {echo " <font color=gold>FLEET COMMANDER</font>";}  //8
  		if($number / 10000 % 10) {echo " <font color=fuchsia>RANCOR</font> ";}  //16
  		if($number / 100000 % 10) {echo " <font color=darkblue>AAT</font> ";}  //32
  		if($number / 1000000 % 10) {echo " <font color=tomato>SITH</font> ";}  //64
  		if($number / 10000000 % 10) {echo " <font color=blue>LSTB(1)</font> ";}  //128
  		if($number / 100000000 % 10) {echo " <font color=blue>LSTB(2)</font> ";}  //256
  		if($number / 1000000000 % 10) {echo " <font color=blue>LSTB(3)</font> ";}  //512
  		if($number / 10000000000 % 10) {echo " <font color=blue>LSTB(4)</font> ";}  //1024
  		if($number / 100000000000 % 10) {echo " <font color=blue>LSTB(5)</font> ";}  //2048
  		if($number / 1000000000000 % 10) {echo " <font color=blue>LSTB(6)</font> ";}  //4096
}

Now if the number is less than, or equal to, 2048 then everything is fine, it's when the number is over 2048 a lot more gets printed.

 

On the attached screenshot I have the numbers on the far right, the decimal number is 3584, which outputs the correct binary code which means the function should only print the 3 lines before the last, but in the screen shot it shows that it also prints the first 2 lines of the code.

 

What am I doing wrong and how can I fix it?  Or what is another solution if I can't get this fixed?

 

James

 

post-78258-0-21122800-1521943999_thumb.jpg

Link to post
Share on other sites
  • Solution

decbin's range depends on whether you are on a 32-bit php version or a 64-bit php version. Your input is fine for both, but converting that binary value back to decimal is the problem.

 

decbin(3584) = "111000000000"

(int)"111000000000" = 111,000,000,000

 

On a 32-bit system that decimal value is too high so you end up with 2,147,483,647 (the maximum value for an integer) when doing % 10.

 

Dividing by 100 gets you low enough to fit within the 32-bit range so you stop having issues at that point.

 

What sort of problem are you actually trying to solve? It looks like you want to test if certain bits are set in a number. If that is the case, then you want to be using the bitwise operators, not decbin.

 

function teams($number){
    if($number & 0x01) {echo " <font color=green>ARENA</font> ";}  //1
    if($number & 0x02) {echo " <font color=maroon>FLEET</font> ";}  //2
    if($number & 0x04) {echo " <font color=yellow>FLEET RESERVE</font>";}  //4
    if($number & 0x08) {echo " <font color=gold>FLEET COMMANDER</font>";}  //8
    if($number & 0x10) {echo " <font color=fuchsia>RANCOR</font> ";}  //16
    if($number & 0x20) {echo " <font color=darkblue>AAT</font> ";}  //32
    if($number & 0x40) {echo " <font color=tomato>SITH</font> ";}  //64
    if($number & 0x80) {echo " <font color=blue>LSTB(1)</font> ";}  //128
    if($number & 0x0100) {echo " <font color=blue>LSTB(2)</font> ";}  //256
    if($number & 0x0200) {echo " <font color=blue>LSTB(3)</font> ";}  //512
    if($number & 0x0400) {echo " <font color=blue>LSTB(4)</font> ";}  //1024
    if($number & 0x0800) {echo " <font color=blue>LSTB(5)</font> ";}  //2048
    if($number & 0x1000) {echo " <font color=blue>LSTB(6)</font> ";}  //4096
}

Link to post
Share on other sites
This thread is more than a year old.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.