Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

unsigned variable problem

Posted on 2004-04-18
7
Medium Priority
?
470 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
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…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

688 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