Problem 1 Write a C program that extracts some bits from an unsigned integer, and prints out the unsigned integer value of these bits. For example, the binary representation of 789 is XXXXXXXXXX...

1 answer below »
4 short programming tasks in C. Prompts are attached as well as relevant lecture slides containing all necessary information.


Problem 1 Write a C program that extracts some bits from an unsigned integer, and prints out the unsigned integer value of these bits.  For example, the binary representation of 789 is 0000 0000 0000 0000 0000 0011 0001 0101 In the binary representation, from the lowest bit, bit 0 is 1, bit 1 is 0, bit 2 is 1, bit 3 is 0… If we extract bit 2 to bit 8, we get 7 bits: 1000101. The unsigned integer value of these 7 bits is 69.  The program reads both the unsigned integer (e.g., 789) and the range (starting bit and ending bit, e.g., 2 and 8) from the user (scanf()), and prints out the value corresponding to these bits (e.g., 69). To get the bits, your program may form a bit mask to "mask off" the bits that are not needed. Then, it shifts the bits to get unsigned integer value. Your program must use bit-wise operations on the binary data to extracts the bits. Translating the data into a string of character '0's and '1's and then scanning the string are not an efficient way and are prohibited.  Problem 2  Write a C program that can find the longest continuous sequence of 1s in the binary representation of an unsigned integer. The integer cannot be 0. So there is at least one 1 in its binary representation. For example, if the integer is 61326, the program will print out 5.  The binary representation of 61326 is  0000 0000 0000 0000 The five 1s are bit 7~ bit 11.  The program reads the unsigned integer from the user, and prints out the range (e.g., "bit 7 ~ bit 11").  Your program must use bit-wise operations on the binary data to find the bits. Translating the data into a string of character '0's and '1's and then scanning the string are not an efficient way and are prohibited.  Problem 3 Write a C program that flips the lower 4 bits of every character in a string (i.e., 1->0 and 0->1 for bits 0~3 in each byte). The program reads the string from the user and prints out the obtained string. Your program can assume that the string length does not exceed 127. Note that your program should not flip the bits in the NULL character (i.e., 0) at the end of the string. Your program must use bit-wise operations to operate the bits in the string. Translating the data into characters ('0's and '1's) and then scanning the characters are not an efficient way and are prohibited.  Problem 4  Write a C program that scans a bit string and prints out the total number of bits that are 0s . The string is provided by the user as a text. Your program can assume that the string length does not exceed 127. Note that your program should not check the bits in the NULL character (i.e., 0) at the end of the string. Your program must use bit-wise operations to scan the bits in the string. Translating the data into characters ('0's and '1's) and then scanning the characters are not an efficient way and are prohibited.  Some Other Instructions In problems 3 and 4, to make scanf read into a string containing space characters, you can use %[^\n] as format string, e.g., scanf("%[^\n]", buf_str); Bitwise operators usually have low precedence.  Use parentheses when necessary.  Refer to this table for operator precedence: https://en.cppreference.com/w/c/language/operator_precedence      CS 288 Intensive Programming Understand your data in memory All data saved in memory is in binary format Accessing using memory addresses Pointers Data type (e.g., signed/unsigned, char, int, float, …) unit length (e.g., 1B, 4B, …) and the way to interpret the bits (sign bit, exponent, value, …) All operations directly handle binary data Arithmetic operations (addition, multiplication, …) Bitwise operations. Bit string: a stream of 0s and 1s. Bit 1 vs. char ‘1’ vs. integer 1 vs. floating point 1 $ cat ./binary.c #include int main() { char c='1'; int i=1; float f=1; printf("%c %i %f\n", c, i, f); } $ gcc -ggdb -o binary ./binary.c $ gdb ./binary (gdb) break 4 (gdb) run (gdb) x/tb &c 0x7fffffffe407: 00110001 (gdb) x/tw &i 0x7fffffffe408: 00000000000000000000000000000001 (gdb) x/tw &f 0x7fffffffe40c: 00111111100000000000000000000000 TypeSize char1 bytes short2 bytes int4 bytes long8 bytes float4 bytes double8 bytes pointer8 bytes size_t8 bytes What do arrays I and f look like in memory? Binary data in memory (explore using gdb) $ cat ./ binary_content.c #include #include main(){ int i[20], value, j; float f[20]; value = -10; for( j = 0; j < 20;="" j++)="" {="" i[j]="value;" f[j]="value;" value="value" +="" 1;="" }="" printf("examine="" memory="" now.\n");="" }="" binary="" data="" in="" memory="" (explore="" using="" gdb)="" $="" gcc="" -ggdb="" -o="" binary_content="" ./binary_content.c="" $="" gdb="" ./binary_content="" (gdb)="" list="" (gdb)="" list="" (gdb)="" break="" 15="" (gdb)="" r="" (gdb)="" x/20dw="" i="" 0x7fffffffe380:="" -10="" -9="" -8="" -7="" 0x7fffffffe390:="" -6="" -5="" -4="" -3="" 0x7fffffffe3a0:="" -2="" -1="" 0="" 1="" 0x7fffffffe3b0:="" 2="" 3="" 4="" 5="" 0x7fffffffe3c0:="" 6="" 7="" 8="" 9="" (gdb)="" help="" x="" examine="" memory:="" x/fmt="" address.="" address="" is="" an="" expression="" for="" the="" memory="" address="" to="" examine.="" fmt="" is="" a="" repeat="" count="" followed="" by="" a="" format="" letter="" and="" a="" size="" letter.="" format="" letters="" are="" o(octal),="" x(hex),="" d(decimal),="" u(unsigned="" decimal),="" t(binary),="" f(float),="" a(address),="" i(instruction),="" c(char),="" s(string)="" and="" z(hex,="" zero="" padded="" on="" the="" left).="" size="" letters="" are="" b(byte),="" h(halfword),="" w(word),="" g(giant,="" 8="" bytes).="" memory="" addreses="" data="" binary="" data="" in="" memory="" (explore="" using="" gdb)="" (gdb)="" x/20fw="" f="" 0x7fffffffe3d0:="" -10="" -9="" -8="" -7="" 0x7fffffffe3e0:="" -6="" -5="" -4="" -3="" 0x7fffffffe3f0:="" -2="" -1="" 0="" 1="" 0x7fffffffe400:="" 2="" 3="" 4="" 5="" 0x7fffffffe410:="" 6="" 7="" 8="" 9="" (gdb)="" x/20dw="" f="" (gdb)="" x/20fw="" i="" (gdb)="" x/20tw="" i="" (gdb)="" x/20tw="" f="" (gdb)="" x/20tw="" &i="" binary="" data="" in="" memory="" can="" be="" interpreted="" in="" different="" ways="" (types).="" the="" same="" data="" in="" memory="" represent="" different="" values="" when="" casted="" into="" different="" types.="" how="" to="" verify="" this="" in="" programs?="" $="" cat="" ./="" binary_content.c="" #include=""> #include main(){ int i[20], value, j, k; float f[20]; unsigned int *p; value = -10; for( j = 0; j < 20;="" j++)="" {="" i[j]="value;" f[j]="value;" value="value" +="" 1;="" }="" p="(unsigned" int="" *)f;="" for="" (="" j="0;" j="">< 5;="" j++="" )="" {="" for="" (="" k="0;">< 4;="" k++)="" printf("%u\t",="" p[j*4+k]);="" printf("\n");="" }="" printf("examine="" memory="" now.\n");="" }="" this="" allows="" us="" to="" interpret="" the="" same="" data="" in="" a="" different="" way.="" (int="" *)="" changes="" the="" type.="" same="" numbers="" printed="" out="" as="" what="" is="" printed="" out="" in="" gdb="" with="" command="" x/20dw="" f="" questions:="" is="" “type”="" information="" saved="" in="" memory?="" if="" it="" is="" saved,="" why="" changing="" types="" is="" allowed?="" if="" it="" is="" not,="" how="" cpu="" knows="" the="" correct="" way="" to="" interpret="" the="" data?="" (add="" vs.="" fadd)="" binary="" vs.="" text="" int="" a="123" ,="" b="234" ;="" c="a+b;" *="" is="" c="" 357?="" */="" int="" a="123," b="234;" c="a+b;" *="" is="" c="" 357?="" */="" write="" a="" c="" program="" that="" saves="" 10="" million="" integers="" into="" a="" file.="" write="" another="" c="" program="" that="" reads="" the="" integers="" out="" from="" the="" file="" into="" an="" array.="" do="" you="" save="" text="" or="" binary="" into="" the="" file?="" saving="" text="" into="" the="" file:="" takes="" much="" more="" time="" to="" read/write,="" uses="" much="" more="" space,="" lacks="" uniformity="" (difficult="" to="" calculate="" the="" count,="" difficult="" to="" calculate="" the="" offset="" of="" a="" particular="" value).="" how="" are="" the="" fields="" in="" a="" structure="" saved="" in="" memory?="" let’s="" explore="" how="" a="" structure="" is="" saved="" in="" memory="" $="" cat="" structure.c="" #include=""> #include struct record{ int index; char name[8]; float score; }; main(){ struct record rec1 = {1, "Tom", 85.5}; printf("Examine memory now.\n"); } Structure saved in memory $ gcc -ggdb -o ./structure ./structure.c $ gdb ./ structure (gdb) list (gdb) list (gdb) break 13 (gdb) r (gdb) x/16bx &rec1 0x7fffffffe3f0: 0x01 0x00 0x00 0x00 0x54 0x6f 0x6d 0x00 0x7fffffffe3f8: 0x00 0x00 0x00 0x00 0x00 0x00 0xab 0x42 (gdb) x/1dw 0x7fffffffe3f0 0x7fffffffe3f0: 1 (gdb) x/4cb 0x7fffffffe3f4 0x7fffffffe3f4: 84 'T' 111 'o' 109 'm' 0 '\000‚ (gdb) x/1fw 0x7fffffffe3fC 0x7fffffffe3fc: 85.5 rec1.index starting from 0x7fffffffe3f0 rec1.name starting from 0x7fffffffe3f4 rec1.score starting from 0x7fffffffe3fC In a program, can we access the data if we know its address and type? Accessing data if you know address and type $ cat ./address_type_2_data.c #include #include struct record{ int index; char name[8]; float score; }; main(){ struct record rec1 = {1, "Tom", 85.5}; int *field1 = (int *)(&rec1); char *field2 = (char *)(&rec1) + 0x4; float *field3 = (float *)((char *)(&rec1) + 0xC); printf("index: %d\n", *field1); printf("name: %s\n", field2); printf("score: %f\n", *field3); } $ ./address_type_2_data index: 1 name: Tom score: 85.500000 How are the elements in a 2D array saved in memory? Let’s explore how 2D and 3D arrays are saved in memory $ cat ./array2d.c #include
Answered 2 days AfterJun 21, 2021

Answer To: Problem 1 Write a C program that extracts some bits from an unsigned integer, and prints out the...

Yohansh answered on Jun 23 2021
131 Votes
assessemnet 86881/Documentation of 86881.docx
Introduction of the Assessment
This assessment mainly focus on the implementation and operations related to bit mask . Bit manipulation improves the time complexity of the program.
Each and every think in programing is in the f
or bits and bytes. This assessment check your ability to do right shift, left shift, and flipping on bits in C language.
Requirements specifications of the assignment
1)Knowledge of Bits and bytes.
2)Left shift and Right shift.
3) Functionalities of bit string.
4)All bit mask operations such as AND , OR , XOR, NOT (&,|,^,~).
Source Code
Problem 1
#include
#include
#include
int main()
{
int num ,l,r;
scanf("%d%d%d", &num,&l,&r);
int i = 0;
unsigned long long int mask = 1;
int ans=0,k=0;
while(i < 32)
{
    int bit=1;
if((num & mask) == 0 )
bit=0;
if(l<=i && i<=r)
{
        int a;
a = (int)(pow(2, k++) + 0.5);
ans+=bit*a;
}
     i++;
mask = mask<<1 ;
}
printf("%d\n",ans);
return 0;
}
Problem 2
#include
#include
#include
int main()
{
int num ;
scanf("%d", &num);
int i = 0;
unsigned long long int mask = 1;
int ans=0;
int count=0,maxi=0;
while(i < 32)
{
    int bit=1;
if((num & mask) == 0 )
{
    bit=0;
    }
if(bit==0)
{
    if(count>maxi)
    {
        maxi=count;
        }
    count=0;
    }
    else
    {
        count++;
    }
    i++;
mask = mask<<1 ;
}
    printf("%d\n",maxi);
return 0;
}
Problem 3
#include
#include
#include
#include
int main()
{
char buf_str[128];
scanf("%[^\n]%*c", buf_str);
int ans=0;
int i=0;
for( i=0;i {
    if((i/4)%2)
    {
         if((((int)buf_str[i])&1)==0)
     {
buf_str[i]='1';
     }
    else
    {
     buf_str[i]='0';
    }
}
}
printf("%s",buf_str);
return 0;
}
Problem 4
#include
#include
#include
#include
int main()
{
char buf_str[127];
scanf("%[^\n]%*c", buf_str);
int ans=0;
int i=0;
for( i=0;i {
    if((((int)buf_str[i])&1)==0)
    {
        ans++;
    }
}
printf("%d\n",ans);
return 0;
}
Application and Implementation
In the first problem we input a number and the left and right boundaries , Then we will check the bit by using left shift operator and if the current bit lies In between left and right boundary we will calculate the new number formed by the bit ranging from left to right.
In...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here