Lecture 8 Flashcards
Bitwise operations in C and Unix Config
Rightmost bit is
the least significant LSB or low-order bit
leftmost bit is
the most significant bit (MSB) or high-order bit
byte is collection of
8 bits
a bit is
a single binary digit
memory
is just an array of bytes
The binary number 01100100 represents
100 ( 2^2 + 2^5 + 2^6)
two’s complement notation
handling of negative numbers in computers
If the leftmost number is 1
then it can be represented as a negative number
11111111 represents
-1
if w1 and w2 are both defined as short int , and w1 is set equal to 25 and
w2 is set equal to 77, then the C statement w3 = w1 & w2; What is the value assigned to w3?
9
w1 0000000000011001 = 25
&
w2 0000000001001101 = 77
———————————-
w3 0000000000001001 = 9
It compares the value in w1 and w3
Bitwise ANDis frequently used for
MASKING operations; where you use a specific binary value (like 3) to selectively “mask” certain bits of another number (here, w1), effectively zeroing out the bits that don’t correspond to 1 in the mask
w1 is 11001010 (an 8-bit binary number), and 3 is 00000011.
Performing w1 & 3 will look
w1: 11001010
3 : 00000011
—————-
w3: 00000010
Is word &= 15; equivalent to word = word & 15; ?
yes
What is the equivalent to Stress = Stress & 100
Stress &= 100;
Is the bitwise AND operator & the same as the logical AND operator &&?
NO
When dealing with 32- of 64-bit computers, which notation is most often used
Hexadecimal
the octal constant of 0177 represents what decimal value
=127 (1 * 64 + 7 * 8 + 7)
to display an integer in octal?
printf( %o)
Convert the decimal number 2345 into hexadecimal
0x929
Convert the decimal number 512 into octal.
1000 (in octal).
Convert Hexadecimal 0x1A3 to Decimal
419
Convert Octal 725 to Decimal
469
Base of Hexidecimal
16
Base of Octal
8
So, if w1 is an unsigned int equal to octal 0431 and w2 is an unsigned
int equal to octal 0152 , then a bitwise OR of w1 and w2 would produce what result
w1 … 100 011 001 0431
| (OR)
w2 … 001 101 010 0152
—————————-
… 101 111 011 0573
which equals 379 in decimal
What is this bitwise operator called:
If either bit is a 1—but not both—the
corresponding bit of the result is a 1; otherwise it
is a 0.
XOR operator ^
If w1 and w2 were set equal to octal 0536 and octal 0266 , respectively,
then the result of w1 Exclusive-ORed with w2 would be?
octal 0750
w1 … 101 011 110 0536
^(XOR)
w2 … 010 110 110 0266
————————–
… 111 101 000 0750
How do you use XOR to swap two integers without using a temporary:
temp = i1;
i1 = i2;
i2 = temp;
i1 ^= i2;
i2 ^= i1;
i1 ^= i2;
What does this complement operator ~ do?
flip the bits of its operand
If w1 is a short int that is 16 bits long, and is
set equal to octal 0122457 , then taking the ones complement of this value produces?
w1 1 010 010 100 101 111 0122457
~w1 0 101 101 011 010 000 0055320
What will be the result of the following C code snippet, and explain the steps involved?
int w1 = 5; // Initial value
w1 «= 2; // Left shift w1 by 2 bits
w1 now holds the value 20
Original: 0101 (5 in decimal)
Shifted: 010100 (after shifting left by 2 bits)
010100 = 20
If w1 is an unsigned int , which is represented in 32 bits, and w1 is set equal to hexadecimal F777EE22 , then shifting w1 one place to the right with the statement w1»_space;= 1; sets w1 equal to?
7BBBF711
w1 1111 0111 0111 0111 1110 1110 0010 0010 F777EE22
w1»_space; 1 0111 1011 1011 1011 1111 0111 0001 0001 7BBBF711
how to access UNIX box
ssh -Y
What is a GUI
a window manager: gnome, KDE, etc. Manages the invocation of processes via mouse and keyboard
What files are commonly used to configure a user’s shell environment in Linux?
.bashrc, .profile, and .login are commonly used configuration files
What command can you use to view all current environment variables in a Bash session?
Use set, env, or printenv to view environment variables.
How can you create your own environment variables in Bash?
Use export VAR=value
to set your own environment variables.
What happens to environment variables defined in a Bash session if not exported?
They only exist in the shell where they are defined and do not persist after closing the session.
What occurs when Bash executes a command?
Bash forks a copy of itself and then uses exec to run the new command
What is the significance of the $PATH variable?
The $PATH variable specifies the directories where executable programs are located, allowing you to run commands without specifying their full paths.
How can you create aliases to simplify command usage in Bash?
Use the alias command, e.g., alias h=history or alias rm=rm -i to create shortcuts.
What is the benefit of using aliases like alias rm=rm -i?
It prompts before deleting files, helping to prevent accidental file deletions.
How can you bypass an alias in Bash?
Use a backslash before the command, e.g., \rm -f to ignore the alias and use the original command.
In which standard directories do most commands reside in a Linux system?
Most commands are located in /bin, /usr/bin, /usr/local/bin, and /opt/X11/bin.
What wildcards can you use in the shell for pattern matching?
Use * (matches any number of characters) and ? (matches a single character) as wildcards in the shell.
How can you find the path to a specific command binary or shell script?
Use which or whereis to locate the command binary or shell script.
How can you search for the end of a line in Vim?
Use $ to search for the end of a line.
What types of files can commands be, apart from binary executables?
Commands can also be shell programs, Python scripts, Perl scripts, and similar types of scripts.
What command can you use to find the location of a binary, header, or source file?
Use the locate command to find the location of files.
How can you access your recent commands in the terminal?
Use the history command, the up-arrow key, or the !command or !number syntax to access commands from the history buffer.
What wildcard does Vim use to represent any single character?
In Vim, the . wildcard represents any single character.
How can you search for the beginning of a line in Vim?
Use ^ to search for the beginning of a line.
What does the Vim command :1, /ending/ do?
It searches from the first line to the line containing the word “ending”.
What does the Vim command :1,$s/me/mine/g accomplish?
It substitutes every occurrence of “me” with “mine” in the entire file (from the first line to the last).
What is the effect of the Vim command :1,$s/^Student Number/SNUM/?
It replaces “Student Number” at the beginning of any line with “SNUM” throughout the entire file.