This question already has answers here:
What's the function of the ~ bitwise operator (Tilde) [duplicate]
(3 answers)
Closed 9 years ago.
Consider:
php > $a = 12; // 1100
php > echo ~$a;
-13
I would expect the inverse of 1100 to be either 0011 (direct) or 11110011 (an entire byte). That would give a result to either 3 or 243. Whence cometh -13?
Again, for good measure, another unexpected result of the same type and explanation:
php > $b = 6; // 0110
php > echo ~$b;
-7
Why -7?
Look at this code:
<?php
$val = 6;
print "$val = ".decbin($val);
print "\n";
$val = ~$val;
print "$val = ".decbin($val);
It prints
6 = 110
-7 = 11111111111111111111111111111001
At first you have 110. As my php uses 32 bits, after inverting all the bits, we get this huge number. As the 1-st bit is 1, php interprets it as a negative value, stored, using two's-complement representation. To find out, the modulus of the negative value, stored in this notation, we
invert the digits:
110
add one to the result:
111
which gives us 7
So, the value is -7
http://www.cs.cornell.edu/~tomf/notes/cps104/twoscomp.html
Why -7?
6 is 00000000000000000000000000000110, so ~6 is ~00000000000000000000000000000110, and that's equal to 11111111111111111111111111111001. Because a signed datatype is used, the first bit indicates whether the number is positive or negative (positive = 0 and negative = 1). Because it is Two's complement, you should convert the binary number to decimal using this way:
Invert the binary number. You get 00000000000000000000000000000110
Convert 00000000000000000000000000000110 (a positive binary number) to a decimal number. You get 6
Add 6 up with one: you get 7
Make it negative: you get -7
Related
This question already has an answer here:
Strange behaviour with numbers that have a leading zero [duplicate]
(1 answer)
Closed 5 years ago.
echo (int)01; //1
echo (int)02; //2
echo (int)03; //3
echo (int)04; //4
echo (int)05; //5
echo (int)06; //6
echo (int)07; //7
echo (int)08; //0
echo (int)09; //0
echo (int)010; //8
echo (int)011; //9
echo (int)012; //10
echo (int)013; //11
(int) was doing right from 01 to 07. But after that it goes wrong. What's the reason of it??
Perhaps 08 is expected to be octal number, like 0x is hexadecimal.
If a number starts with 0 it is consider an octal number
and since these numbers range from 0 to 7 only. You get a 0 since 8%8 = 0
Reference: http://php.net/manual/en/language.types.integer.php
A side note on number systems
Have you wondered why the next number to 9 is 10? and why in the binary system the sequence is 0, 1, 10, 11... ? And why the octal system allows only values from 0 to 7?
It is because number systems usually increment values based on modulo logic on the base.
For example take the binary number system. Since it is base 2, it can only contain values 0, 1 since 0%2 = 0, 1%2 = 1, but 2%2 is again 0
So whenever a greater number comes, say 3. Its value in binary is (increment by one in the preceding place) (put the modulo in the existing place)
So the value of 3 in the binary system is (0+1) (3%2) = 11
Though this is not the exact logic, just putting it here for a beginner reference
This question already has answers here:
What are bitwise operators?
(9 answers)
Closed 7 years ago.
I dont get how the following codes work?
function odd($var){
return ($var & 1);
}
echo odd(4); /* returns and print 0 */
echo odd(5); /* returns and print 1 */
this function returns true if argument is an odd number
and returns false if argument is an even number. How it works ?
Odd numbers in binary always have a least-significant bit (LSB) of 1. That is why your code
function odd($var){
return ($var & 1);
}
returns true on odd numbers. Here are your examples from your question:
(decimal) 4 & 1 = (binary) 100 & 001 = (binary) 000 = (decimal) 0 = false
(decimal) 5 & 1 = (binary) 101 & 001 = (binary) 001 = (decimal) 1 = true
Another way to think of it is
100 (decimal 4) - an even number
AND 001 (decimal 1)
= 000 (decimal 0) - return false
and
101 (decimal 5) - an odd number
AND 001 (decimal 1)
= 001 (decimal 1) - return true
bitwise comparison already says what it does: it compares the numbers bit by bit.
If we take 4 bits, the bit representation of 4 is: 0100. The bit representation of 5 is 0101. When we compare this with & (and), it returns the bits which are both set.
0100 & 0001 = 0000 (no bits are the same)
0101 & 0001 = 0001 (only Least Significant Bit (LSB) is 1)
It is masking off all bits except 0. & is the and operator. And 1 is 000000000001 in binary. So it works as it is named.
This question already has answers here:
what is 0050 and why echo 0050 result 40 [duplicate]
(2 answers)
Closed 9 years ago.
While coding I got an unexpected result.
I'm unsure how to ask this question so I'll put in my code and result:
$variable = 012;
$variable2 = 12;
$variable3 = '012';
When I echo out the variables:
Expected result:
$variable: 12
$variable2: 12
$variable3: 012
Result:
$variable: 10
$variable2: 12
$variable3: 012
What is happening here?
See the documentation of the integers of php
Then you see the following:
To use octal notation, precede the number with a 0
So octal 12 is decimal 10
Little bit more info of the docs
<?php
$a = 1234; // decimal number
$a = -123; // a negative number
$a = 0123; // octal number (equivalent to 83 decimal)
$a = 0x1A; // hexadecimal number (equivalent to 26 decimal)
$variable is Octal
$variable2 is int
$variable3 is string
This question already has answers here:
How to pad single-digit numbers with a leading 0
(7 answers)
Closed 11 months ago.
I have the value as $title =10 and I want to minus it with 01.So my code is:
echo $minus = round(($title - 01),2);
I want the result $minus = 09 but this code not like this it still have the result $minus=9. Anyone help me please,Thanks.
The problem is that PHP is not strongly typed, and round() returns a number, which automatically has the leading zero stripped. Try:
$minus = "0" . round(($title - 01),2);
PHP is evaluating your 0-prefixed numbers to their base value -- 04 and 01 are 4 and 1 respectively.
If you want them to be output with a leading 0, try using a number formatter, or string padding or simply append them to the string, "0"
What's happening is that round() returns an integer. Which means it won't have any 0's before it. If you want it to return 0's before it, try
str_pad(round(($title - 1), 2), 2, "0");
That will make it always append a 0 before the number if it's only 1 number, but if it's 10 or 15 or something, it won't append a 0
echo str_pad(intval($title) - 1, 2, "0", STR_PAD_LEFT);
This will pad your result with a 0 if the result is only one digit; otherwise, it will not pad. For a leading zero always, you can replace the 2 with strlen($title)
Try this..
$i=04-01;
echo sprintf('%02s', $i);
I need to be able to get the last digit of a number.
i.e., I need 2 to be returned from: 12.
Like this in PHP: $minute = substr(date('i'), -1) but I need this in Python.
Any ideas
last_digit = str(number)[-1]
Use the % operator:
x = 12 % 10 # returns 2
y = 25 % 10 # returns 5
z = abs(-25) % 10 # returns 5
Python distinguishes between strings and numbers (and actually also between numbers of different kinds, i.e., int vs float) so the best solution depends on what type you start with (str or int?) and what type you want as a result (ditto).
Int to int: abs(x) % 10
Int to str: str(x)[-1]
Str to int: int(x[-1])
Str to str: x[-1]