Bit Operations/Optimization Flashcards
List 4 types of programs that need to perform operations at the bit level:
- Systems programs(including compilers and OS’s)
- Encryption programs
- Graphics programs
- Programs for which fast execution and/pr efficient use of space is critical
What are the bitwise shift operators and how do they work?
- << left shift
- for each bit that is “shifted off” the left end of i, a zero bit enters at the right
- >> right shift
- if the value shifted is nonnegative, zeros are added at the left as needed
(may be of any integar type, including char)
unsigned short i, j;
What would the result(in binary) be of the following operations:
- i = 13;
- j = i << 2;
- j = 1 >> 2;
- 0000000000001101
- 0000000000110100
- 0000000000000011
How do you read binary numbers (what’s the basic formula as compared to base 10)?
Represent 16, 7 and 21 in binary
In general, it is powers of 2, starting from the right at 2^0 + 2^1, etc. This is the same way our base ten math work. 21 = 1 X 10^0 + 2 * 10^1
16 in binary = 0000000000010000
7 in binary = 0000000000000111
21 in binary = 0000000000010101
What is the binary representation of the following:
- i = 13;
- i <<= 2;
- i >>= 2;
- 0000000000001101
- 0000000000110100
- 0000000000001101
What is the precedence (order/priorty) or a bitwise shift operator?
What does i << 2 + 1 mean?
It is lower than arithmatic.
it means i << 3
What are the bitwise “compliment”, “and”, “exclusive or” and “inclusive or” operators? In general, what do they do?
- ~ = compliment (switches bits from what they were, e.g. 0 to 1
- & = and (works like a regular and statment, is true if bits are both 1)
- | = inclusive or (works like a regular or, if one of the items is 1/true then the statement is 1/true)
- ^ = exclusive or (if the bit is set and are different then it sets it to true, if the bits are the same, then it sets it to false)
unsigned short, i, j, k;
What affect would the following operations have in binary?
- i = 21;
- j = 56;
- k = ~i;
- k = i & j;
- k = i ^ j;
- k = i | j;
- 0000000000010101
- 0000000000111000
- 1111111111101010
- 0000000000010000
- 0000000000101101
- 0000000000111101
Evaluate the following using binary.
- i = 21;
- j = 56;
- i &= j;
- i ^= j;
- i |= j;
- 0000000000010101
- 0000000000111000
- 0000000000010000
- 0000000000101000
- 0000000000111000
In general how do hexadecimal numbers work?
What is the number 1BC?
Hex numbers are 16 bits, 0-9 + A-F
0-9 is obviously 0-9 and A - F is 10-15
1BC in hex is 444
- How does C know we are using hex numbers?
- How do you print in hex format with printf?
- how to print min of 4 digits?
- How many bits is each hex number represented by?
- hex numbers in C are prefixed by 0x (captilization does not matter)
- %x
- %04x
- hex numbers are represented by 4 bits (24 = 16)
What is Binary Code Decimal (BCD). Provide the example for the following:
- 3
- 4
- 7
- B
The 4 bit representation of the decimal digit
- 0011
- 0100
- 0111
- 1011
In general, how does it work to use bitwise operators to access bits?
- What is the easiest way to set bit 4 of i?
- How would i |= 1 << j be equivalent?
- if j has a value of 3, what would 1 << j be in hex and binary?
The easiest way to set bit 4 of i is to use to or the value of i with the hex constant 0x0010:
- i = 0x0000; // this is 0 in hex
- 00000000000000000
- i |= 0x0010; //this is 16 in hex
- 00000000000010000 (this is 16 in hex now)
- hex = 0x008, binary 0000000000001000
If we wanted to clear bit 4 of i how would that be done?
i = 0x00ff;
We would need a number whose only different bit is in the 5th position and use the & operator.
i in binary is 00000000111111111
i &= 0x0010 (16)
in binary is 0000000011101111 (and flipping the only different bit, bit 4 (in the 5th position)
- In general, how do you test a bit?
- How would you test if bit 4 is set?
- How would you test if bit j is set?
4.
- Using an if statement the & operator and a specific number corresponding to the specific bit to test.
- if ( i & 0x0010)
- if ( i & 1 << j )