Bitwise operations..., Exercises of Computer science

practice for bitwise opertations and other problems to help

Typology: Exercises

2021/2022

Uploaded on 02/27/2024

combat-1
combat-1 🇺🇸

1 document

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Bitwise Operators Handout 2
1. Consider the byte x = 1101 0011. What is the result of each of the following bitwise
operations? (answer in binary)
x >> 3 (logical) 0001 1010 ~x 0010 1100
x >> 3 (arithmetic) 1111 1010 !x 0000 0000
x << 4 0011 0000
x * 8 (this is essentially x << 3) 1001 1000
2. Use shifts and additions/subtractions to represent the following:
x * 64 => x << 6
x * 7 => (x << 3) – x OR (x << 2) + (x << 1) + x
x * 23 => (x << 4) + (x << 3) - x
3. What are the results of the following in hexadecimal?
x = 1011 0110 y = 1010 0010
x & y 0xA2
x | y 0xB6
x ^ y 0x14
pf2

Partial preview of the text

Download Bitwise operations... and more Exercises Computer science in PDF only on Docsity!

Bitwise Operators Handout 2

1. Consider the byte x = 1101 0011. What is the result of each of the following bitwise

operations? (answer in binary)

x >> 3 (logical) 0001 1010 ~x 0010 1100 x >> 3 (arithmetic) 1111 1010 !x 0000 0000 x << 4 0011 0000 x * 8 (this is essentially x << 3) 1001 1000

2. Use shifts and additions/subtractions to represent the following:

x * 64 => x << 6 x * 7 => (x << 3) – x OR (x << 2) + (x << 1) + x x * 23 => (x << 4) + (x << 3) - x

3. What are the results of the following in hexadecimal?

x = 1011 0110 y = 1010 0010 x & y 0xA x | y 0xB x ^ y 0x

4. Solve the following bitwise puzzle to implement the isNegative function

// this function returns 1 is x < 0, otherwise it returns 1 // legal operations:! ~ & ^ | + << >> // max number of total operations allowed: 6 int isNegative(int x){ int y = x >> 31; return !!y // note that this is one of many solutions that could be possible for this problem! }

5. Complete the following:

Create a mask with Shifting and Bitwise Ops char mask =? << ?; Use the mask to Set Bit 5 of value to a 0 value = value? mask; // Do not use XOR char mask = ~(1 << 5); value = value & mask;