Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

unsigned variable problem

Posted on 2004-04-18
7
Medium Priority
?
475 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 1000 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses how to create an extensible mechanism for linked drop downs.
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

916 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