Problem

(Computer architecture: bit-level operations) A short value is stored in 16 bits.Write a p...

(Computer architecture: bit-level operations) A short value is stored in 16 bits.

Write a program that prompts the user to enter a short integer and displays the 16 bits for the integer. Here are sample runs

(Hint : You need to use the bitwise right shift operator (>>) and the bitwise AND operator (&), which are covered in Appendix E.)

Bitwise Operations

To write programs at the machine-level, often you need to deal with binary numbers directly and perform operations at the bit-level. C++ provides the bitwise operators and shift operators defined in the following table.

Operator

Name

Example

Description

&

bitwise AND

10101110&10010010 yields 10000010

The AND of two corresponding bits yields a 1 if both bits are 1.

 

Bitwise inclusive OR

10101110 |10010010 yields 10111110

The OR of two corresponding bits yields a 1 if either bit is 1.

A

Bitwise exclusive OR

10101110A 10010010 yields 00111100

The XOR of two corresponding bits yields a 1 only if two bits are different.

~

One's complement

~10101110 yields 01010001

The operator toggles each bit from 0 to 1 and from 1 to 0.

 

Left shift

10101110 << 2 yields 10111000

Shift bits in the first operand left by the number of the bits specified in the second operand, filling with 0s on the right.

>>  

Right shift for unsigned integer

1010111010101110 >> 4 yields 0000101011101010

Shift bit in the first operand right by the number of the bits specified in the second operand, filling with zeros on the left.

>> 

Right shift for signed integer

 

The behavior depends on the platform. Therefore, you should avoid right shift signed integers.

All the bitwise operators can form bitwise assignment operators such as A=, |= <<=, and >>=.

Step-by-Step Solution

Request Professional Solution

Request Solution!

We need at least 10 more requests to produce the solution.

0 / 10 have requested this problem solution

The more requests, the faster the answer.

Request! (Login Required)


All students who have requested the solution will be notified once they are available.
Add your Solution
Textbook Solutions and Answers Search
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT