shifting(<<,>>) operator doesnot work well in php - php

i am trying to do simple program using php logical operator, when i try to invert the binary value using ~ operator it giving some symbol, instead of actual inverted value also when i try to shift the bits using using <<,>> it doesn't give expected value,help me?
here is the simple coding
<?php
$n=10;
$bin=decbin($n);
echo $bin;
//for onces complement
$com_value=~$bin; //this statement prints some IIIIIIIIIIIIIIII symbol instead of inverted value
echo $com_value;
$shift_value=$bin<<1;
echo $shift_value; //this printing 2020 as result for binary number of ten 1010
?>
help me to get correct value,i have to complete one assignment using that, thanks in advance

decbin returns a string, i.e. a sequence of 0 and 1 characters, with the binary representation of $n. This is not correct as you want to execute ~ and << on the binary value, which is simply $n. Although $n "looks decimal" in the source code, it is stored as 0's and 1's in your memory.
Try to remove that conversion into a string and use $n directly.

Related

inval return wrong number if the number start with zero

I have a number 00101 when I print out this number (or using it for my purpose) I got 65, I've tried intval() but it also returns 65. Can anyone explain to me why? And what is the easiest way to get 00101, or 101?
I would say you are using an invalid type of number, there is bits type (see the list of types in https://www.php.net/manual/en/langref.php)
if you run the following
$a = array(10101, 11100, 11010, 00101);
var_dump($a);
you will see that PHP convert your number to int 65
so maybe you want to use strings?
You will get 101 from string '00101' when passing to intval function. However an integer number does not have start with leading 0; PHP does not get it as decimal number.

Why does this happen in php when we multiple string by number it always gives zero(0)?

Suppose we have a string $str = "a"; and number $num = 2;
$str = 'a';
$num = 2;
echo $str*$num;
Output:
0
When performing arithmetic operations on a string operand, PHP will try to convert the string to a number.
It does this by looking for digits at the beginning of the string and will try to convert them into a value. If there are no digits, the value will be zero.
(There's an edge case for strings containing e or E (scientific notation), but that's not relevant here.)
Good Question.
Same i did ask to my teacher when i was in collage,
The answer is.
String * int= infinity; //According to scientific calculator answer is infinity.
but we need to continue our so program it provide 0.
it is made by code by default answer.
Simply said the string will be converted to an integer with a value of 0. This will include most of the cases when only alphabetic values are used. If you try to add a integer value at the beginning of the string it would in theory become a integer of that value.
I would recommend to read Why PHP Strings Equal Zero or Comparison Operators
Maybe you are looking for str_repeat, instead doing looping for that, its a default value that php serve to you, or you need to cast A into integer . When you try to do calculation for data that is not in Integer/float data type. Usually PHP auto-typecast the variables. In some cases it wont. Then we have to type cast it manually

php scientific notation format

Trying to format a scientific number in PHP:
sprintf(%'1.2E',$var)
This gets me to 5.01E+1
I am trying to print 2 digits after the + sign
The parser requires the number format to be:
5.01E+01 instead of 5.01E+1
Is it possible to achieve this format with sprintf?
Is there any other method that can achieve this?
Thanks for looking
I couldn't find a way to do it solely with sprintf but I believe the following would be closer to the "correct" way to do it. The following calculates the exponent from the base-10 logarithm.
You can then pass the original value (dividing by 10 to the power of the exponent) and the exponent to the sprintf function as a float and an integer respectively. You can force the positive + sign remembering that it counts towards the character / padding length.
function scientificNotation($val){
$exp = floor(log($val, 10));
return sprintf('%.2fE%+03d', $val/pow(10,$exp), $exp);
}
demo / test cases :
scientificNotation(5.1); // 5.10E+00
scientificNotation(50.1); // 5.01E+01
scientificNotation(500.1); // 5.00E+02
scientificNotation(0.0051); // 5.10E-03
There is no built-in method as far as I know. But with a little bit of Regex black magic you can do something like this:
preg_replace('/(E[+-])(\d)$/', '${1}0$2', sprintf('%1.2E',$var));
Notice that I just wrapped your call to sprintf() with an appropriate call to preg_replace(). If the regular expression does not match it will leave the output from sprintf() as is.
The above answers both work great.
Also found out I can use:
$var_formatted = shell_exec("printf '%1.2E' $var");
which could be the cleanest given the script has the permissions to execute commmands.
The E+nn format is the default output format for the linux shell printf.
From the shell:
:~$ echo `printf '%1.2E' 500`
5.00E+02
:~$ echo `printf '%1.2E' 5`
5.00E+00
:~$ echo `printf '%1.2E' 0.05`
5.00E-02

strange behavior of php in implicit convert

strangely the following code return true!
if ('1'==1 && '014'==016)
echo 1;
and output is
1
can anyone tell me why '014' is equal to 016? and how to solve this problem? and if it is possible avoid to do a explicit convert because the data type all the time is vary.
i'm using PHP Version 5.3.8-ZS5.5.0
In PHP, using an integer value with a leading zero leads PHP to assume it's an octal number. Octal 016 is equal to decimal 14.
Using a string such as '014' is implicitly cast to a decimal 14 when used in a comparison with another integer.
See http://php.net/manual/language.types.integer.php
you need to convert string values to integer first, like this:
if(intval('1')==1 && intval('014')==16)
echo 1;
see intval()
Try this one.
<?php
if (ltrim((int)'1',0) == ltrim(1,0) && ltrim((int)'014',0)== ltrim('016', '0'))
echo 1;

issue in reversly printing a number in php [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What does a leading zero do for a php int?
I am just a beginner in php. I tried to write program for print the given number as reverse. For example if i use 123 as input the result should be 321. I tried the following code. It works fine. But if the given input is 0123 the output is 38. I couldn't correct it. How can i correct my code? here is my code.
<?php
$n=123;
$b=0;
while($n>=1)
{
$b=$b*10+$n%10;
$n=$n/10;
}
echo $b;
?>
When you add a 0 to the beginning of your number, PHP treats the number as octal.
Octal 123 is decimal 83, which correctly changes to 38.
Just use the dynamic nature of PHP not much caring about the difference between numbers and strings:
echo strrev(123);
http://www.php.net/manual/en/function.strrev.php
Yes, it's because the numbers that are starting with 0 are considered in base 8, so 0123 is in fact 83 in base 10.
So, your algorithm is correct for integer numbers - if you want the revert of 0123 to be 3210, you could simply revert it as a string and you can simply use the strrev function
I think your math is the problem. You should have parenthesis around the multiplier and mod.
<?php
$num=6541020;
$revnum=0;
do{
$revnum=($revnum *10)+($num % 10);
$num=(int)($num / 10 );
}while($num>0);
echo $revnum;
?>
http://www.weberdev.com/get_example.php3?ExampleID=4879
you could use something like this:
<?php
$rprint = function ($nr) use (&$rprint)
{
echo $nr%10;
if($nr > 10)
{
$rprint($nr/10);
}
};
$rprint(12364);
?>
Edit 1:
OR you could use strings instead of numbers. As I suppose it was already suggested.
(I was a bit out of the subjects)
the integer with the leading zero is always treated as octal in php. So just make it as a string and use the buitin php function strrev()

Categories