Lecture 8 Flashcards

Bitwise operations in C and Unix Config

1
Q

Rightmost bit is

A

the least significant LSB or low-order bit

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

leftmost bit is

A

the most significant bit (MSB) or high-order bit

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

byte is collection of

A

8 bits

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

a bit is

A

a single binary digit

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

memory

A

is just an array of bytes

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

The binary number 01100100 represents

A

100 ( 2^2 + 2^5 + 2^6)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

two’s complement notation

A

handling of negative numbers in computers

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

If the leftmost number is 1

A

then it can be represented as a negative number

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

11111111 represents

A

-1

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

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?

A

9
w1 0000000000011001 = 25
&
w2 0000000001001101 = 77
———————————-
w3 0000000000001001 = 9

It compares the value in w1 and w3

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Bitwise ANDis frequently used for

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

w1 is 11001010 (an 8-bit binary number), and 3 is 00000011.

Performing w1 & 3 will look

A

w1: 11001010
3 : 00000011
—————-
w3: 00000010

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Is word &= 15; equivalent to word = word & 15; ?

A

yes

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is the equivalent to Stress = Stress & 100

A

Stress &= 100;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Is the bitwise AND operator & the same as the logical AND operator &&?

A

NO

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

When dealing with 32- of 64-bit computers, which notation is most often used

A

Hexadecimal

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

the octal constant of 0177 represents what decimal value

A

=127 (1 * 64 + 7 * 8 + 7)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

to display an integer in octal?

A

printf( %o)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

Convert the decimal number 2345 into hexadecimal

A

0x929

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

Convert the decimal number 512 into octal.

A

1000 (in octal).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

Convert Hexadecimal 0x1A3 to Decimal

A

419

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

Convert Octal 725 to Decimal

A

469

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

Base of Hexidecimal

A

16

24
Q

Base of Octal

A

8

25
Q

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

A

w1 … 100 011 001 0431
| (OR)
w2 … 001 101 010 0152
—————————-
… 101 111 011 0573

which equals 379 in decimal

26
Q

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.

A

XOR operator ^

27
Q

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?

A

octal 0750

w1 … 101 011 110 0536
^(XOR)
w2 … 010 110 110 0266
————————–
… 111 101 000 0750

28
Q

How do you use XOR to swap two integers without using a temporary:

temp = i1;
i1 = i2;
i2 = temp;

A

i1 ^= i2;
i2 ^= i1;
i1 ^= i2;

28
Q

What does this complement operator ~ do?

A

flip the bits of its operand

29
Q

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?

A

w1 1 010 010 100 101 111 0122457
~w1 0 101 101 011 010 000 0055320

29
Q

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

A

w1 now holds the value 20

Original: 0101 (5 in decimal)
Shifted: 010100 (after shifting left by 2 bits)

010100 = 20

30
Q

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&raquo_space;= 1; sets w1 equal to?

A

7BBBF711

w1 1111 0111 0111 0111 1110 1110 0010 0010 F777EE22
w1&raquo_space; 1 0111 1011 1011 1011 1111 0111 0001 0001 7BBBF711

31
Q

how to access UNIX box

A

ssh -Y

32
Q

What is a GUI

A

a window manager: gnome, KDE, etc. Manages the invocation of processes via mouse and keyboard

33
Q

What files are commonly used to configure a user’s shell environment in Linux?

A

.bashrc, .profile, and .login are commonly used configuration files

34
Q

What command can you use to view all current environment variables in a Bash session?

A

Use set, env, or printenv to view environment variables.

35
Q

How can you create your own environment variables in Bash?

A

Use export VAR=value
to set your own environment variables.

35
Q

What happens to environment variables defined in a Bash session if not exported?

A

They only exist in the shell where they are defined and do not persist after closing the session.

36
Q

What occurs when Bash executes a command?

A

Bash forks a copy of itself and then uses exec to run the new command

36
Q

What is the significance of the $PATH variable?

A

The $PATH variable specifies the directories where executable programs are located, allowing you to run commands without specifying their full paths.

37
Q

How can you create aliases to simplify command usage in Bash?

A

Use the alias command, e.g., alias h=history or alias rm=rm -i to create shortcuts.

37
Q

What is the benefit of using aliases like alias rm=rm -i?

A

It prompts before deleting files, helping to prevent accidental file deletions.

38
Q

How can you bypass an alias in Bash?

A

Use a backslash before the command, e.g., \rm -f to ignore the alias and use the original command.

39
Q

In which standard directories do most commands reside in a Linux system?

A

Most commands are located in /bin, /usr/bin, /usr/local/bin, and /opt/X11/bin.

40
Q

What wildcards can you use in the shell for pattern matching?

A

Use * (matches any number of characters) and ? (matches a single character) as wildcards in the shell.

40
Q

How can you find the path to a specific command binary or shell script?

A

Use which or whereis to locate the command binary or shell script.

40
Q

How can you search for the end of a line in Vim?

A

Use $ to search for the end of a line.

41
Q

What types of files can commands be, apart from binary executables?

A

Commands can also be shell programs, Python scripts, Perl scripts, and similar types of scripts.

42
Q

What command can you use to find the location of a binary, header, or source file?

A

Use the locate command to find the location of files.

43
Q

How can you access your recent commands in the terminal?

A

Use the history command, the up-arrow key, or the !command or !number syntax to access commands from the history buffer.

44
Q

What wildcard does Vim use to represent any single character?

A

In Vim, the . wildcard represents any single character.

45
Q

How can you search for the beginning of a line in Vim?

A

Use ^ to search for the beginning of a line.

46
Q

What does the Vim command :1, /ending/ do?

A

It searches from the first line to the line containing the word “ending”.

47
Q

What does the Vim command :1,$s/me/mine/g accomplish?

A

It substitutes every occurrence of “me” with “mine” in the entire file (from the first line to the last).

48
Q

What is the effect of the Vim command :1,$s/^Student Number/SNUM/?

A

It replaces “Student Number” at the beginning of any line with “SNUM” throughout the entire file.