# Bit-Level Operations In the 1800s, George Boole proposed a logic-system based on two values: 0…

Bit-Level
Operations

In the 1800s, George Boole proposed a
logic-system based on two values: 0 and 1. We will work through how these
operations are exposed in C, and by extension the underlying system. The first
set of operations are performed by treating the integer as a bit-vector.

7. One of the steps to negating a signed
integer was to invert all of the bits. This operation, complement or
“_”, can be applied to any integer. For each integer, convert it from
hexadec- imal to binary and then apply the operation. Show the binary version
and then convert the answer back to hexidecimal.

• Example: -(0xF0F0) +-(1111 0000 1111 0000)
+0000 1111 0000 1111 + 0x0F0F

-(0x3C3C)

-(OxCAFE) .

-(0x0000)

8. Fill in the following table. In the left
column is a value (let’s call it z). Convert it from decimal to binary, then
use bitwise AND to show (z & Oxl):

Dec Bin X & 0x1

-2

-1

0 0000 0000

1

2

9. For which numbers was
the value & 0xl not 0000? What is a common property of these integers?

10. Many times in
systems programming, we want to test if a flag value is set in a given bit-
pattern. The programmer will commonly use (X & FLAG) == FLAG. Give an
explanation of this expression. When does it return true (1)? When does it
return false (0)?

11. Systems programmers will also regularly
have to combine flag values. Describe how OR (1) is used in the following
example, which creates a new file for writing:

open (filename,
(O_WRONLY | O_CREAT | O_TRUNC), …);

(Hint: O_WRONLY,
O_CREAT, and O_TRUNC are each flags, meaning only one bit is set to 1 in each,
at a different bit position.)

12. We know that in
signed integer datatypes (such as int and short int in C) the leftmost bit (or
most-significant bit, MSB) is the sign bit. Let’s say you want to write an
expression in C which masks out all bits in x except the sign bit. Fill in the
blank in this expression with the correct constant value to use as a bitmask:

int signbit = x &

13. Now consider the
situation, like in Data Lab, where you cannot use arbitrarily large constants.
Only constants 0 through 255 (0x00 through Oxff) are allowed. Now, how would
you write the code (like above) that masks out all bits in x except the sign
bit? (Hint: utilize bit-shift operators)

int signbit = x &
14.

In both cases above (which should yield the
same value), what is the value of signbit if x is positive? If x is Negative?

15. Let’s say you need
to convert signbit from above into a value that is always either 0 or 1. Fill
in the blank below to complete the expression, in terms of signbit:

(This one is tricky.
Remember how arithmetic shifts work!)

int isNegative = (
signbit >> —-) &

Could you also write
this expression in terms of x?

int isNegative = ( x
>> & ;

The blanks above should be filled with either
binary or logical operators, or constants.

## Calculate the price of your order

550 words
We'll send you the first draft for approval by September 11, 2018 at 10:52 AM
Total price:
\$26
The price is based on these factors:
Number of pages
Urgency
Basic features
• Free title page and bibliography
• Unlimited revisions
• Plagiarism-free guarantee
• Money-back guarantee
On-demand options
• Writer’s samples
• Part-by-part delivery
• Overnight delivery
• Copies of used sources
Paper format
• 275 words per page
• 12 pt Arial/Times New Roman
• Double line spacing
• Any citation style (APA, MLA, Chicago/Turabian, Harvard)

# Our guarantees

Delivering a high-quality product at a reasonable price is not enough anymore.
That’s why we have developed 5 beneficial guarantees that will make your experience with our service enjoyable, easy, and safe.

### Money-back guarantee

You have to be 100% sure of the quality of your product to give a money-back guarantee. This describes us perfectly. Make sure that this guarantee is totally transparent.

### Zero-plagiarism guarantee

Each paper is composed from scratch, according to your instructions. It is then checked by our plagiarism-detection software. There is no gap where plagiarism could squeeze in.

### Free-revision policy

Thanks to our free revisions, there is no way for you to be unsatisfied. We will work on your paper until you are completely happy with the result.