0

Recently I found a weird thing in shell script. It behave different with how I think when I using ~ (bitwise not operation). Following is sample code in shell:

function items() {
    local expected="$1"
    local input="$2"
    try "$expected" "int main(int argc, int argv) { $input }"
}
function expr() {
    local expected="$1"
    local input="$2"
    items "$expected" "exit($input);"
}
expr 18 "~237"

18 is expected value for ~237, but in c is -238.

binary in 18 is  : .000010010
binary in 237 is : .011101101

I think result in C is right but why behave like this?

Steven
  • 811
  • 4
  • 23

0 Answers0