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
Solved

unsigned variable problem

Posted on 2004-04-18
7
459 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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
PHP Installer 5 37
Decrypt string by php 7 46
Undefined Variable: loop_limit, Undefined Variable: number 3 17
Extracting store locations from Google maps or site 2 21
This article discusses how to create an extensible mechanism for linked drop downs.
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

789 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