Solved

unsigned variable problem

Posted on 2004-04-18
7
458 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
error log using ftp 7 39
Couls you point a way to make Codeigniter to consider the UTF-8 characters in config.php? 14 40
mysql date time 14 29
PHP not parsing ' character 12 38
Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
This article discusses how to create an extensible mechanism for linked drop downs.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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…

776 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