Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Programming assignment. Needs to be written in C language. Please include notes. Thank you Program Description: This program will be written in the language, and

Programming assignment. Needs to be written in C language. Please include notes. Thank you image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
Program Description: This program will be written in the language, and will print integer values in binary (base-2). Integers on our machines are 32-bit values, this means each is a set of 32 binary values that encodes the integer value. 1101011 11011011110|1 1 1 0 110 111 110 1101110110101 The left-most bit is the most "signficant" in that it contributes the most to the magnitude of the value This is identical to base-10 values such as 924 where the 9 represents 900 and contributes more than the 2 or the 4. Numbers in general are coefficients of powers of some base. So 924 9.10^2+2 . 10^1 + 4 . 10^0. with binary numbers the base is 2, so 1001 = 1 2^3+0+2^2+02^1 + 1. 2018 +1-9). A computer stores numbers internally in this binary form, but there is no easy way to access the bits. Computers today are byte-addressable, meaning that only groups of 8-bits can be addressed in memory. To read individual bits we need to use bitwise operations such as AND, OR, XOR, NOT, and shifting. In this assignment we will shift individual bits out of the right side of the integer. We are also going to use AND to determine the value of the right most least significant) bit. A logical AND operation will combine two binary values and produce a new value with a one in each position where the two input values had a one. A zero will be placed otherwise. A truth table of this operation: Input AND 0 0 00 01 10 11 0 1 If we have two binary numbers, say 1011 and 0110, we can bitwise and them by ANDing each corresponding bit. If we have two binary numbers, say 1011 and 0110, we can bitwise and them by ANDing each corresponding bit. 1011 0110 0010 We can use this by ANDing a value with a 1 in the most significant position. If the result is a 1, the most significant value was 1. Otherwise, it was a zero. 0 0 1 1 0 0 1 1 1 0 1 0 1 1 1 0 1 1 1 0 1 0 1 1 1 0 1 0 1 1 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 This will allow us to test the left-most bit. How do we get to the other bits? We could create new binary values that mask out the individual bits, but this is a lot of work. It would be nice to be able to do this in a loop. If we are able to test (and print) the left bit, we could create a loop and do that 32 times if we were also able to shift each of the bits over one space to the left each pass. We can either shift numbers to the lett or to the right. A shift moves all of the digits in the number in a sertain direction letting some digits "fall off" the end, and filling in new values depending on the Significant value was 1. Otherwise, it was a zero. 1 1 1 0 0 1 1 1 0 1 0 1 1 1 0 1 1 1 0 1 0 1 1 1 0 1 0 1 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 This will allow us to test the left-most bit. How do we get to the other bits? We could create new binary values that mask out the individual bits, but this is a lot of work. It would be nice to be able to do this in a loop. If we are able to test (and print) the left bit, we could create a loop and do that 32 times if we were also able to shift each of the bits over one space to the left each pass. We can either shift numbers to the left or to the right. A shift moves all of the digits in the number in a certain direction, letting some digits "fall off" the end, and filling in new values depending on the direction of the shift. There are also two types of shifts (arithmetic and logically, but this will not matter for this assignment. Bits that are shifted out of a number are lost forever. When we left shift a number, a new rero will appear in the rightmost digit to replace the previous value. Thus if we start with 1011 and shift it one position to the left, we are left with 0110. The leading one is lost and a new zero is shifted in on the right When we right shift a value, the bits move in the opposite direction. A logical right shift will introduce a zero in the left-most bit (vacated during the shift). An arithmetic right shift will introduce a copy of the previous left-most bit. Thus, a logical right shift of 1011 results in 0101, and an arithmetic right shift results in 1101. There is no difference in arithmetic and logical left shifts (a zero is shifted into the vacant place in each case) How do we tell the computer we wish to AND a value with a one in the left bit? If we knew the integer value with a bit in that position, we could use it. If we were able to use binary numbers individually, we could do that. Neither is easy in C We instead use base-16 (hexadecimal). Each hexadecimal digit is an encoding (1-1 equivalent) of a group of 4 bits We instead use base-16 (hexadecimal). Each hexadecimal digit is an encoding (1-1 equivalent) of a group of 4 bits. Decimal ON 000 Hex 0 2 4 4 6 Binary 0000 0010 0100 0110 1000 1010 1100 1110 6 8 10 12 14 Decimal 1 3 5 7 9 11 13 15 Hex 1 3 5 7 9 B D F Binary 0001 0011 0101 0111 1001 1011 1101 1111 00 E if we want a bit in the rightmost position, we see that 8 is the value to choose. But this only accounts for 4/32 bits. For a 32-bit value, we fill the rest with O's: 0x80000000 Therefore, ANDing with Ox80000000 will result in a zero value if the most significant bit is o, and a non- pero value otherwise. This is enough to print a l'ora o Therefore, ANDing with 0x80000000 will result in a zero value if the most significant bit is 0, and a non- zero value otherwise. This is enough to print a 'l'or a 'o'. Then shift left one position using the "

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Advances In Databases And Information Systems 25th European Conference Adbis 2021 Tartu Estonia August 24 26 2021 Proceedings Lncs 12843

Authors: Ladjel Bellatreche ,Marlon Dumas ,Panagiotis Karras ,Raimundas Matulevicius

1st Edition

3030824713, 978-3030824716

More Books

Students also viewed these Databases questions