05-17-2007
This has been very helpful. It reveals to me that UNIX is storing signed integer values just like most programming languages I am familiar with. The leading 1's for negative numbers would indicate that the highest order bit is a sign bit. Therefore 2#1010 is 10 and -2#1010 is -10 which would be represented as:
111....1110110 - depending on the bit size used.
Therefore I am suspecting the course is not correct when it claims ((~ 2#1001)) evaluates to 2#110, but that it actually evaluates to:
"-2#1010".
If UNIX used unsigned integers in this case then it would evaluate to a very large number depending on the bit size used. Something like 2#111...11110110. From your analysis I do not suspect this is the case.
There is one more thing I would like to verify. The course has the following question:
What do you think is the output of the following piece of code?
((x = 2#1101 & 2#110))
((y = ~x))
print - $y
The answer given is 11, as in decimal eleven or 2#1011.
From what I understand I think the answer should be -5 as in -2#101.
I plan to inform the course provider of these possible issues but I would like to make sure there is an issue. Thanks for your help.
9 More Discussions You Might Find Interesting
1. UNIX for Advanced & Expert Users
can anybody write a program to divide a number by another number using bitwise operators (9 Replies)
Discussion started by: areef4u
9 Replies
2. Shell Programming and Scripting
Guys,
Pl suggest me how to ignore a path in find command... I am aware of using "!" for other option... but how can i use it like this exampl...
I want to search something for in / but not in
Pl help.
Thanks.. (2 Replies)
Discussion started by: clx
2 Replies
3. Programming
Hi
Suppose we have these code lines:
#define _IN_USE 0x001 /* set when process slot is in use */
#define _EXITING 0x002 /* set when exit is expected */
#define _REFRESHING 0x004
...
1 main () {
2
3 unsigned r_flags =_REFRESHING;
4
5 if (r_flag &... (3 Replies)
Discussion started by: Puntino
3 Replies
4. Shell Programming and Scripting
Hi all,
I've been searching the internet and can't find an answer to this. Im trying to figure out how to negate a whole expression befor an if. I'll explain below.
Let's say
x=0
y=1
] (this is false) I would like to negate this whole boolean condition. To negate the above... (4 Replies)
Discussion started by: rethymno19
4 Replies
5. Homework & Coursework Questions
Hello, have anybody any idea, how to negate some expression?
I have code that greps somethnig and I want filter from it some results...How to make it?
IDontWantThisExpression=$1
grep 'some regex' | grep '$IDontWantThisExpression' testfile.txt
Thanks for any advices.
Faculty of... (2 Replies)
Discussion started by: Dworza
2 Replies
6. FAQ Submission Queue
The purpose of this article is revealing the unrevealed parts of the bitwise XOR.
As we aware, the truth table for the XOR operator is :
A B A^B
0 0 0
0 1 1
1 0 1
1 1 0
For example , 1^2 will be calculated as given below:
First the operands... (1 Reply)
Discussion started by: pandeesh
1 Replies
7. Shell Programming and Scripting
please any one can suggest me how to use bitesie || opearator to do this
#initallize a=0 b=0
#condition
if then
a=0
else a=1
fi
#bitwise or opeartion b = a || b
Please view this code tag video for how to use code tags when posting code and data. (3 Replies)
Discussion started by: Palaniappan
3 Replies
8. Shell Programming and Scripting
Hello,
I want to compute the bitwise number of matches in pairwise fashion for all columns. The problem is I have 18486955 rows and 750 columns. Please help with code, I believe this will take a lot of time, is there a way of tracking progress?
Input
Org1 Org2 Org3
A A T
A ... (9 Replies)
Discussion started by: ritakadm
9 Replies
9. Shell Programming and Scripting
$ ls -1
a.1
b.1
x_a.1
x_b.1
$ ls -1 *
b.1
x_a.1
x_b.1
$ ls -1 **
a.1
b.1
x_a.1
x_b.1The last result is not as expected.
Why?
Thanks. (2 Replies)
Discussion started by: carloszhang
2 Replies
BINDEC(3) 1 BINDEC(3)
bindec - Binary to decimal
SYNOPSIS
number bindec (string $binary_string)
DESCRIPTION
Returns the decimal equivalent of the binary number represented by the $binary_string argument.
bindec(3) converts a binary number to an integer or, if needed for size reasons, float.
bindec(3) interprets all $binary_string values as unsigned integers. This is because bindec(3) sees the most significant bit as another
order of magnitude rather than as the sign bit.
PARAMETERS
o $binary_string
- The binary string to convert
Warning
The parameter must be a string. Using other data types will produce unexpected results.
RETURN VALUES
The decimal value of $binary_string
EXAMPLES
Example #1
bindec(3) example
<?php
echo bindec('110011') . "
";
echo bindec('000110011') . "
";
echo bindec('111');
?>
The above example will output:
51
51
7
Example #2
bindec(3) interprets input as unsigned integers
<?php
/*
* The lesson from this example is in the output
* rather than the PHP code itself.
*/
$magnitude_lower = pow(2, (PHP_INT_SIZE * 8) - 2);
p($magnitude_lower - 1);
p($magnitude_lower, 'See the rollover? Watch it next time around...');
p(PHP_INT_MAX, 'PHP_INT_MAX');
p(~PHP_INT_MAX, 'interpreted to be one more than PHP_INT_MAX');
if (PHP_INT_SIZE == 4) {
$note = 'interpreted to be the largest unsigned integer';
} else {
$note = 'interpreted to be the largest unsigned integer
(18446744073709551615) but skewed by float precision';
}
p(-1, $note);
function p($input, $note = '') {
echo "input: $input
";
$format = '%0' . (PHP_INT_SIZE * 8) . 'b';
$bin = sprintf($format, $input);
echo "binary: $bin
";
ini_set('precision', 20); // For readability on 64 bit boxes.
$dec = bindec($bin);
echo 'bindec(): ' . $dec . "
";
if ($note) {
echo "NOTE: $note
";
}
echo "
";
}
?>
Output of the above example on 32 bit machines:
input: 1073741823
binary: 00111111111111111111111111111111
bindec(): 1073741823
input: 1073741824
binary: 01000000000000000000000000000000
bindec(): 1073741824
NOTE: See the rollover? Watch it next time around...
input: 2147483647
binary: 01111111111111111111111111111111
bindec(): 2147483647
NOTE: PHP_INT_MAX
input: -2147483648
binary: 10000000000000000000000000000000
bindec(): 2147483648
NOTE: interpreted to be one more than PHP_INT_MAX
input: -1
binary: 11111111111111111111111111111111
bindec(): 4294967295
NOTE: interpreted to be the largest unsigned integer
Output of the above example on 64 bit machines:
input: 4611686018427387903
binary: 0011111111111111111111111111111111111111111111111111111111111111
bindec(): 4611686018427387903
input: 4611686018427387904
binary: 0100000000000000000000000000000000000000000000000000000000000000
bindec(): 4611686018427387904
NOTE: See the rollover? Watch it next time around...
input: 9223372036854775807
binary: 0111111111111111111111111111111111111111111111111111111111111111
bindec(): 9223372036854775807
NOTE: PHP_INT_MAX
input: -9223372036854775808
binary: 1000000000000000000000000000000000000000000000000000000000000000
bindec(): 9223372036854775808
NOTE: interpreted to be one more than PHP_INT_MAX
input: -1
binary: 1111111111111111111111111111111111111111111111111111111111111111
bindec(): 18446744073709551616
NOTE: interpreted to be the largest unsigned integer
(18446744073709551615) but skewed by float precision
NOTES
Note
The function can convert numbers that are too large to fit into the platforms integer type, larger values are returned as float in
that case.
SEE ALSO
decbin(3), octdec(3), hexdec(3), base_convert(3).
PHP Documentation Group BINDEC(3)