* To Do #1: Set the bit field [15..12] of data_in to all 1s. Print the result. */ /* To Do #2: Clear the bit field [8..1] of data_in to all 0s. Print the result */ /* To Do #3: Invert bit 31 (the MSB) of data_in . Print the result */ /* To Do #4: Swap bit field [31..24] (upper byte) with bit field [7..0] (lower byte) of data_in. * Print the result. */ /* To Do #5: Use the Bitwise operations to convert the number to be 0xabcdef00. Print the result. */
/******************************************************************************
Online C Compiler.
Code, Compile, Run and Debug C program online.
Write your code in this editor and press "Run" button to compile and execute it.
*******************************************************************************/
/HW on Bitwise operations
* History:
* 2020-08-23, sac, original
* 2020-08-25, sac, Modified comments for operating on the original data_in value and not cascading results.
* Changed the bit field location of "TO DO #2" to prevent the shift right/shift left solution.
* Modified other comments for clarity.
* 2021-8-12, adding "To Do # 5"
*/
/* Notes and Requirements:
* 1. uint32_t, uint16_t, uint8_t are defined integer types with defined width.
* These type definitions are in stdint.h. Very handy for unambiguously creating
* unsigned integers with a well known width.
* 2. Bit fields are defined as a range of bit positions enclosed in brackets: [23..16]
* Another way of saying it is this bit field is 8 bits wide at position 16.
* 3. If the program has an infinte loop, end the program by hitting "<ctrl>C" in the console
* window or click on the Stop button. Don't panic. It may take awhile for the web site to respond.
* 4. I suggest saving your work often. Either sign in to the site or copy-paste
* from the editor window to a text editor on your PC (Notepad) or use the Download function.
* 5. Not required but feel free to write functions for your operations. They can go in this main file or annother file.
* 6. Sections for you to write have a comment starting with "To Do: "
* 7. Comment your code! Over commenting is fine (for now).
* 8. Use the original value of data_in for each manipulation. DO NOT "Cascade" the result.
* 9. All bits not in the specified bit field are to remain unchanged.
*/
#include <stdio.h>
#include <stdint.h>
/* Feel free to use a ".h" file. Include it here */
/* Define any macros here. For example, bit masks or bit positions.
* Good practice is to not bury constant values in the code but give them a name.
* For example:
* #define XYZ_MASK 0x0000e000
* #define ABC_BIT_LOC 5
*/
int main()
{
uint32_t data_in; /* uint32_t is a defined type found in stdint.h */
uint32_t data_out;
/* add your variables here */
/* example of printf formatting for output of an 8 digit hexdecimal number. Here is
* the result in the console window:
*
* Sample hex number output: 0x000000ff
* Enter a hex number (8 digits)>abcd
* Here is what you input: 0x0000abcd
*
*/
data_out = 0xff;
printf("Sample hex number output: 0x%8.8x\n", data_out);
/* example of scanf formatting for input of an 8 digit hexdecimal number */
printf("Enter a hex number (8 digits)>"); /* print a prompt for the input */
scanf("%x", &data_in); /* input the number */
printf("Here is what you input: 0x%8.8x\n", data_in); /* print input for verification */
/* Infinte loop to input a hex number, perform various operations on it and
* print the result.
*
* Print a relevant description with each result. For example,
* "Lower/Upper Byte Swap: "
* "Set a bit field: "
* etc....
*/
printf("\nStarting the input processing loop\n");
for (;;){
printf("Enter a hex number (8 digits)>");
scanf("%x", &data_in);
/* IMPORTANT!!!!!
* Use the original value of data_in for each manipulation. DO NOT "Cascade" the result.
* All bits not in the specified bit field are to remain unchanged
*/
/* To Do #1: Set the bit field [15..12] of data_in to all 1s. Print the result. */
/* To Do #2: Clear the bit field [8..1] of data_in to all 0s. Print the result */
/* To Do #3: Invert bit 31 (the MSB) of data_in . Print the result */
/* To Do #4: Swap bit field [31..24] (upper byte) with bit field [7..0] (lower byte) of data_in.
* Print the result.
*/
/* To Do #5: Use the Bitwise operations to convert the number to be 0xabcdef00. Print the result.
*/
/* To Do #6: Produce your own bit manipulation. Get creative. Consider a loop
* to perform clever bit patterns. Print the result.
*/
}
return 0;
}
Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 1 images