Script started on Wed Oct 2 12:30:01 2024 mweeks@air:C_ch5$ vi system_example.c mweeks@air:C_ch5$ cat system_example.c #include #include // defines "system" int main (int argc, char *argv[]) { int returnValue; returnValue = system("echo hello world"); printf("The command returned with value %d.\n", returnValue); return 0; } mweeks@air:C_ch5$ gcc system_example.c mweeks@air:C_ch5$ ./a.out hello world The command returned with value 0. mweeks@air:C_ch5$ echo hello world hello world mweeks@air:C_ch5$ mweeks@air:C_ch5$ echo "Calling pwd" Calling pwd mweeks@air:C_ch5$ pwd /Users/mweeks/Desktop/csc3320/programs/C_ch5 mweeks@air:C_ch5$ echo "done" done mweeks@air:C_ch5$ mweeks@air:C_ch5$ vi call_pwd.sh mweeks@air:C_ch5$ cat call_pwd.sh echo "Calling pwd" pwd echo "done" mweeks@air:C_ch5$ ls -l call_pwd.sh -rw-r--r-- 1 mweeks staff 50 Oct 2 12:42 call_pwd.sh mweeks@air:C_ch5$ echo Try to run without execute access Try to run without execute access mweeks@air:C_ch5$ ./call_pwd.sh bash: ./call_pwd.sh: Permission denied mweeks@air:C_ch5$ chmod 744 call_pwd.sh mweeks@air:C_ch5$ ls -l call_pwd.sh -rwxr--r-- 1 mweeks staff 50 Oct 2 12:42 call_pwd.sh mweeks@air:C_ch5$ ./call_pwd.sh Calling pwd /Users/mweeks/Desktop/csc3320/programs/C_ch5 done mweeks@air:C_ch5$ vi example_shell_script.bash mweeks@air:C_ch5$ cat example_shell_script.bash #!/bin/bash # # The first lines says that this is a bash shell script. # The second line should be a blank comment. # You should notice that the computer ignores text after the poundsign. # -MCW Oct 2024 # echo "Here is the working dir" pwd mweeks@air:C_ch5$ ls -l *bash -rw-r--r-- 1 mweeks staff 245 Oct 2 12:45 example_shell_script.bash mweeks@air:C_ch5$ chmod 744 example_shell_script.bash mweeks@air:C_ch5$ ls -l *bash -rwxr--r-- 1 mweeks staff 245 Oct 2 12:45 example_shell_script.bash mweeks@air:C_ch5$ ./example_shell_script.bash Here is the working dir /Users/mweeks/Desktop/csc3320/programs/C_ch5 mweeks@air:C_ch5$ vi binary.txt mweeks@air:C_ch5$ cat binary.txt unsigned binary decimal hex 0000 0 0 0001 1 1 0010 2 2 0011 3 3 0100 4 4 0101 5 5 0110 6 6 0111 7 7 1000 8 8 1001 9 9 1010 10 A 1011 11 B 1100 12 C 1101 13 D 1110 14 E 1111 15 F mweeks@air:C_ch5$ echo 01010010 in hex: 0101 0010 maps to 52 01010010 in hex: 0101 0010 maps to 52 mweeks@air:C_ch5$ echo C7 hex in binary: 1100 0111 C7 hex in binary: 1100 0111 mweeks@air:C_ch5$ echo 42 in decimal equivalent to 0042 42 in decimal equivalent to 0042 mweeks@air:C_ch5$ echo 11101 convert to hex, you may want to look up 1110 first, but that does not work because then you only have 1 left 11101 convert to hex, you may want to look up 1110 first, but that does not work because then you only have 1 left mweeks@air:C_ch5$ echo "pad 0's to the left to form 1 1101 to make 0001 1101" pad 0's to the left to form 1 1101 to make 0001 1101 mweeks@air:C_ch5$ cat bitwise.c #include int main() { int x=1,y=2,z=0; printf("x=1,y=2,z=0 \n"); z=x&y; printf("z=x&y; z is %d \n", z); z=x|y; printf("z=x|y; z is %d \n", z); z=x^y; printf("z=x^y; z is %d \n", z); z=x<<1; printf("z=x<<1; z is %d \n", z); z=y>>2; printf("z=y>>2; z is %d \n", z); z=~x; printf("z=~x; z is %d \n", z); return 0; } mweeks@air:C_ch5$ gcc bitwise.c mweeks@air:C_ch5$ ./a.out x=1,y=2,z=0 z=x&y; z is 0 z=x|y; z is 3 z=x^y; z is 3 z=x<<1; z is 2 z=y>>2; z is 0 z=~x; z is -2 mweeks@air:C_ch5$ mweeks@air:C_ch5$ echo x is 1, in binary it is 0001 x is 1, in binary it is 0001 mweeks@air:C_ch5$ echo y is 2, in binary it is 0010 y is 2, in binary it is 0010 mweeks@air:C_ch5$ echo "0001 AND 0010" > bit_examples.txt mweeks@air:C_ch5$ vi bit_examples.txt mweeks@air:C_ch5$ vi bitwise.c mweeks@air:C_ch5$ gcc bitwise.c mweeks@air:C_ch5$ ./a.out x=9,y=10,z=0 z=x&y; z is 8 z=x|y; z is 11 z=x^y; z is 3 z=x<<1; z is 18 z=y>>2; z is 2 z=~x; z is -10 mweeks@air:C_ch5$ vi bit_examples.txt mweeks@air:C_ch5$ vi bit_examples.txt mweeks@air:C_ch5$ vi bitwise.c mweeks@air:C_ch5$ cat bitwise.c #include int main() { int x=9,y=10,z=0; printf("x=%d,y=%d,z=%d \n", x, y, z); z=x&y; printf("z=x&y; z is %d \n", z); z=x|y; printf("z=x|y; z is %d \n", z); z=x^y; printf("z=x^y; z is %d \n", z); z=x<<1; printf("z=x<<1; z is %d \n", z); z=y>>2; printf("z=y>>2; z is %d \n", z); z=~x; printf("z=~x; z is %d or %x in hex \n", z, z); return 0; } mweeks@air:C_ch5$ gcc bitwise.c mweeks@air:C_ch5$ ./a.out x=9,y=10,z=0 z=x&y; z is 8 z=x|y; z is 11 z=x^y; z is 3 z=x<<1; z is 18 z=y>>2; z is 2 z=~x; z is -10 or fffffff6 in hex mweeks@air:C_ch5$ vi bitwise.c mweeks@air:C_ch5$ gcc bitwise.c mweeks@air:C_ch5$ cat bitwise.c #include int main() { int x=9,y=10,z=0; printf("x=%d,y=%d,z=%d \n", x, y, z); z=x&y; printf("z=x&y; z is %d \n", z); z=x|y; printf("z=x|y; z is %d \n", z); z=x^y; printf("z=x^y; z is %d \n", z); z=x<<1; printf("z=x<<1; z is %d \n", z); z=y>>2; printf("z=y>>2; z is %d \n", z); z=~x; printf("z=~x; z is %d or %x in hex \n", z, z); z=~2; printf("z=~2; z is %d or %x in hex \n", z, z); return 0; } mweeks@air:C_ch5$ ./a.out x=9,y=10,z=0 z=x&y; z is 8 z=x|y; z is 11 z=x^y; z is 3 z=x<<1; z is 18 z=y>>2; z is 2 z=~x; z is -10 or fffffff6 in hex z=~2; z is -3 or fffffffd in hex mweeks@air:C_ch5$ vi bit_examples.txt mweeks@air:C_ch5$ cat bit_examples.txt 9 10 decimal 1001 AND 1010 1001 1010 ---- 1000 = 8 decimal 1001 OR 1010 1001 1010 ---- 1011 = 11 decimal 1001 XOR 1010 1001 1010 ---- 0011 = 3 decimal a b XOR ------- 0 0 | 0 0 1 | 1 1 0 | 1 1 1 | 0 x=9,y=10,z=0 z=x&y; z is 8 z=x|y; z is 11 z=x^y; z is 3 z=x<<1; z is 18 z=y>>2; z is 2 z=~x; z is -10 x is 9 which is 1001 binary y is 10 which is 1010 binary shift x to the left by 1 bit, we get 10010 = 1*2^4 + 0 + 0 + 1*2^1 + 0 = 16 + 2 = 18 shift y to the right by 2 bits, we get 0010 = 2 x is 0..01001 binary, when we invert (NOT) the bits, we get 1..10110 z=~2; z is -3 or fffffffd in hex 2 is 0..0010 in binary, when we invert (NOT) the bits, we get 1..1101 -1 is 1..1111 do the 1's comp: 0..0000 to get 2's comp we add 1 to 1's comp: 0..0000 + 1 = 0..0001 in 2's complement, what is the 4-bit value 1000? 3 is 0011 in binary. What is -3? 1's comp 1100, add 1: 1101, which is -3 mweeks@air:C_ch5$ exit exit Script done on Wed Oct 2 13:47:37 2024