Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re^2: What does ">>" do? (And other stat questions)

by three18ti (Scribe)
on Nov 01, 2013 at 00:39 UTC ( #1060670=note: print w/ replies, xml ) Need Help??


in reply to Re: What does ">>" do? (And other stat questions)
in thread What does ">>" do? (And other stat questions)

Hello,

Thanks for taking the time to debug and respond.

Could you please give me a little insight to what this means:

DB<4> printf "%04o", $mode ; 100755 DB<5> printf "%04o", $mode & 07777; 0755

I think you're trying to show me what happens when you binary "&" 33261 and 07777? maybe that doesn't make any sense because the 07777 isn't present in DB<4> but is in DB<5>. I read in a link above that "&" has lower precedence, so that would bean that "printf "%04o", $mode" is evaluated first? But that doesn't make any sense since printf would do the printing... Ok, I thought I was starting to understand... But I don't think I do.


Comment on Re^2: What does ">>" do? (And other stat questions)
Download Code
Re^3: What does ">>" do? (And other stat questions)
by Laurent_R (Parson) on Nov 01, 2013 at 11:37 UTC

    The $mode value is 33261. I wanted to show what happens when you print it with the octal formatting string (prints 100755) and when you do it after having modified it with the binary and (&). And & has a higher precedence than the comma. As you can see below, adding parens to guarantee the right precedence does not modify the output::

    DB<1> $mode = 33261; DB<2> printf "%04o", $mode; 100755 DB<3> printf "%04o", $mode & 07777; 0755 DB<4> printf "%04o", ($mode & 07777); 0755 DB<5>
Re^3: What does ">>" do? (And other stat questions)
by Laurent_R (Parson) on Nov 01, 2013 at 12:07 UTC

    From your other answers, I think I can help you further by showing the same numbers as above in binary form:

    DB<5> printf "%04b", $mode ; 1000000111101101 DB<6> printf "%04b", 07777; 111111111111 DB<7>

    So, 33261 is 1000000111101101 in binary representation, and 07777 is 111111111111 (or the equivalent 0000111111111111) in binary representation. If we now do manually a bitwise and (&) between these two numbers, we do this:

    1000000111101101 & 0000111111111111 ================ = 0000000111101101

    We get a 1 in the result only at places where we have a 1 in both numbers above. This is in effect applying a mask to cancel out the four binary digits on the left in the original number. The same operation under the debugger:

    DB<8> $c = $mode & 07777; DB<9> printf "%04b", $c; 111101101 DB<10>

    If I now print that same $c number in its octal representation:

    DB<10> printf "%04o", $c; 0755 DB<11>

    Is it clearer in your mind now?

      Awesome! Thanks so much for the explanation.

      I think I understand everything except for why we're essentially throwing away the first four bits. Why are they there to begin with? And why don't we need them?

      Thanks again, this is awesome

        The file mode given by stat[2] consists of two distinct pieces of information: the file type and the file permissions. If you want to see only the permissions, you have to mask off the file type portion of the mode. This is what the bitwise and (&) with 07777 does, as shown in my previous post.

        The file type corresponds to the first character displayed on a ls -l command under the Unix shell prompt:

        $ ls -l /etc/passwd -rw-r--r-- 1 Laurent root 841 9 mai 23:24 /etc/passwd
        The initial dash (-) displayed above says it is a regular file, a d would indicate a directory, a l a symbolic link, an s a socket, etc. This information is the file type displayed in the first half byte (four bits) of the file mode reported by stat[2].

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1060670]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (8)
As of 2014-09-22 12:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (191 votes), past polls