Jump to content

Binary Calculations


swindonshaun

Recommended Posts

Hello there folks,

 

This is my first time posting to such a forum, so please be gentle with me!

 

What I am trying to do is write a script which represents the days of the week as binary (7 bits, each representing one day).

 

I can do this fine. My problem comes when I want to apply the ~ (bitwise NOT operator). It turns it into a 32 bit number, but it gets the desired result at the end of the number e.g.

 

In: 1100000

Out: 11111111111111111111111110011110

 

I have been looking high and low to see if I am doing something wrong. I have cast the variables as (int).

 

I think I understand why it behaves like this, and thought I had come up with a solution, which involving ANDing the output to 0000..1111 (25 zeroes and 7 ones), but the result is always 0. What am I doing wrong? Is there some way of declaring a number as binary as you would with hexadecimal?

 

Many thanks and apologies if this is a dumb question.

 

Shaun

Link to comment
Share on other sites

It turns it into a 32 bit number

 

It already was.

 

Is there some way of declaring a number as binary as you would with hexadecimal?

 

No, but you can just write it in base 8, base 10 or base 16, which PHP has a syntax for. Internally in memory any number is stored as binary regardless, so the base n expansion you use when displaying it is irrelevant.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.