Solved

unsigned variable problem

Posted on 2004-04-18
7
456 Views
Last Modified: 2012-05-04
I have an alghoritm written in C. Few functions use unsigned variables, just like unsigned int, unsigned char etc. I just wonder if it's possible for me to translate that code to PHP as all numeric variable types in PHP are signed.
Is there any easy way to solve that or do i have to convert value to unsigned after every operation?
How can i make PHP to use variable as an 'unsigned char' and not 'char'?


Just one example:

unsigned char calculo4_a (char caracter)
{
        unsigned char aux1, aux2, aux3, temp;
        int i;

        aux1=0;
        aux2=7;
        temp=0;
       
        for (i=0; i<8; i++)
        {
                aux3=caracter;
                aux3=aux3>>aux2;
                aux3&=1;
                aux3=aux3<<aux1;
                temp|=aux3;
                aux1++;
                aux2--;
        }
        return temp;
}
0
Comment
Question by:Lukasz Lach
  • 6
7 Comments
 
LVL 12

Expert Comment

by:venkateshwarr
ID: 10854888
It doesn't matter whether it is int or unsigned int.
0
 
LVL 12

Expert Comment

by:venkateshwarr
ID: 10854908
In C char size is 8 bits that the maximum number possible is 255.
If you use  a variable in php, it is implicitly understood as int if you assign a int... the maximum is way more that 255.
So you need not worry about it.

function calculo4_a ($caracter)
{
        $aux1=0;
        $aux2=7;
        $aux3=0;
        $temp=0;
       
        for ($i=0; $i<8; $i++)
        {
                $aux3=$caracter;
                $aux3=$aux3>>$aux2;
                $aux3 &=1;
                $aux3 =$aux3<<$aux1;
                $temp |=$aux3;
                $aux1++;
                $aux2--;
        }
        $temp;
}

$x = calculo4_a (10);
0
 
LVL 12

Expert Comment

by:venkateshwarr
ID: 10854954
Discard the above post, I was wrong...
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 12

Expert Comment

by:venkateshwarr
ID: 10855013
try this code.. it should work...

$mask = 0x00FF;

function calculo4_a ($caracter)
{
        $aux1=0;
        $aux2=7;
        $aux3=0;
        $temp=0;
       
        for ($i=0; $i<8; $i++)
        {
                $aux3=$caracter;
                $aux3=($aux3>>$aux2) & $mask ;
                $aux3 =($aux3 & 1) & $mask ;
                $aux3 =($aux3<<$aux1) & $mask ;
                $temp = ($temp|$aux3) & $mask ;
                $aux1 = ($aux1 + 1) & $mask ;
                $aux2 = ($aux2 -1) & $mask ;
        }
        $temp;
}

$x = calculo4_a (10);
0
 
LVL 12

Expert Comment

by:venkateshwarr
ID: 10855015
Correction,

$mask = 0x000F;
0
 
LVL 5

Author Comment

by:Lukasz Lach
ID: 10857466
and how the mask would look for 'integer' and 'shortint'?
0
 
LVL 12

Accepted Solution

by:
venkateshwarr earned 250 total points
ID: 10861724
If you give the mask as
$mask = 0x000F;
all the upper bits are erased and made as 0's.

So if want it as 16-bit integer, you also need to pass the mask value to the method
In this case, you have to specify
$mask = 0x00FF;
for 32bit $mask = 0xFFFF;

0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now