C PROGRAM
When you print out the old and new bitsets, which are of type unsigned char, please use the %p control character in the printff statement rather than %x or %d or %c. The compiler will complain, but we don't always listen to them anyway. The difference is it will print the bitset out in hex with a preceeding %0x.
unsigned char bitset = 0x14 ;
printf("Bitsrt is %p\n", bitset) ; results in Bitset is 0x14, which is what we want.
How to obtain input from the user and assign it to an unsigned char ?
One way to do this as follows:
unsigned int Hoboo ; unsigned char bitset ;
printf("Please enter your bitset using either 0x$$ or $$ where $ is a digit in base 16 (0 - F) -->") ;
scanf("%x", &Hoboo) ; // interprets input as hex format
bitset = (unsigned char) Hoboo ;
Whatever the user enters will be interpreted as a hex number. So entering 21 is 21 hex or 33 decimal. Similarly, the user could enter 0x21 which is also 33 decimal.
Find the code for the above question below, read the
comments provides in the code for better understanding. If found
helpful please do upvote this.
Note : In the code below bit 1 is the LSB
Code
//stdio is standard library for basic I/O opearertion
#include <stdio.h>
#include<stdlib.h>
#include<string.h>//for string opertions
//this method checks if the given bit ata a particular position is 0 or 1
//note that bit 1 is the LSB
int isset(unsigned char bitset, int pos){
int val=bitset;//convert the bitset to int
//now if 2^pos-1 & val == 1 then bit is set else not
if (val & (1 << (pos - 1)))
return 1;
else
return 0;
}
//sets the bit at a given postion to 1
//note that bit 1 is LSB
void addbit(unsigned char *bitset, int pos){
int val=*bitset;//get teh decimal number of the bitset
val |= 1 << pos-1; //or with 2^pos-1 sets the bit at the given positon
*bitset=(unsigned char)val;//changes the bitset to the modified value
}
//unsets the bit at a given postion to 1
//note that bit 1 is LSB
void delbit(unsigned char *bitset, int pos){
int val=*bitset;//get teh decimal number of the bitset
val &= ~(1 << pos-1); //and with the negation with 2^pos-1 unsets the bit at the position
*bitset=((unsigned char)val);//changes the bitset to the modified value
}
//print all teh bits in teh bitset
void printBits(unsigned char bitset){
char output[8];//stores the bits in a character array
itoa(bitset, output, 2);//convert the value to binary
strrev(output);//reverse the array so that 0th index is the LSB
printf("1st Bit is the LSB");
//iterate and print all the bits
for(int i=0;i<strlen(output);i++){
printf("\nThe bit in position %d id %c",i+1,output[i]);
}
}
//maind river code of the program
int main()
{
//to get user input
unsigned int Hoboo;
unsigned char bitset;//bitset for the user input
printf("Please enter your bitset using either 0x$$ or $$ where $ is a digit in base 16 (0 - F) -->");
scanf("%x", &Hoboo); // interprets input as hex format
bitset = (unsigned char)Hoboo;
//run a loop until user exits it
while(1){
int ch;
//show choices to the user
printf("\nPlease choose your option 0 - ISSET , 1 = ADD , 2 = DEL , 3 = PRINT , -1 = QUIT : ");
scanf("%d",&ch);
if(ch!=-1){
if(ch==0){
int pos;
printf("\nEnter the bit position to check(1 is th LSB) :");
scanf("%d",&pos);
int val=isset(bitset,pos);//call the isset method to check if bit is set or not
if(val==0) //val=0 menas bit is set else not set
printf("Bit in position %d was not set\n",pos);
else
printf("Bit in position %d was set\n",pos);
}
//this is for settign bit at the given pos to 1
else if(ch==1){
int pos;
printf("\nEnter the bit position to set(1 is th LSB) :");
scanf("%d",&pos);
printf("\nOld Bitset %p ",bitset);
addbit(&bitset, pos);
printf(" , New Bitset %p",bitset);
}
//this is for settign bit at the given pos to 0
else if(ch==2){
int pos;
printf("\nEnter the bit position to unset(1 is th LSB) :");
scanf("%d",&pos);
printf("\nOld Bitset %p ",bitset);
delbit(&bitset, pos);
printf(" , New Bitset %p",bitset);
}
//this if for printing teh bits
else if(ch==3){
printBits(bitset);
}
}
else{
exit(0);
}
}
printf("%p\n\n",bitset);
printBits(bitset);
int chk=isset(bitset , 1);
printf("\n%d",chk);
}
Output
Check the output carefully , it is same as expected
C PROGRAM When you print out the old and new bitsets, which are of type unsigned...
Using Microsoft Visual Studio. 1) Complete the following C++ program by adding more line of code for 8-bit signed array, 16-bit unsigned array, 16-bit signed array, 32-bit signed array and 32-bit signed array. 2) Fill in all the blanks in Table 1 using your completed code, following the hints provided within the table. 3) Fill in all the blanks in Table 2 using your completed code, following the hints provided within the table. C++ Program #include <stdio.h> #include <iostream> int...
Convert the C program into a C++ program.Replace all C input/output statements with C++ statements (cin, cout, cin.getline) . Re-make the func function by the following prototype: void func( double, double &); #include int user_interface(); double func(double); void print_table(); int main (int argc, char *argv[]) { print_table(user_interface()); return 0 ; } int user_interface(int val){ int input = 0; printf("This function takes in x and returns an output\n"); printf("Enter Maximum Number of X:"); scanf("%d", &input);...
C++ HELP I need help with this program. I have done and compiled this program in a single file called bill.cpp. It works fine. I am using printf and scanf. Instead of printf and scanf use cin and cout to make the program run. after this please split this program in three files 1. bill.h = contains the class program with methods and variables eg of class file class bill { } 2. bill.cpp = contains the functions from class...
C programming Question1 (a) Write a C program that will print out all command line arguments, in reverse order, one per line. Prefix each line with its index. 6 marks] (b) Consider this C code snippet int a- 100 int b- 42; inte p- &a; int q-b; p qi printf ("%d %d\n" ,a,*p); When this code is executed, what numbers will it print? [2 marks] (c) Consider this C program int main(int argc,char argv) char* target- "Apple" char vord[100] printf...
C++ HELP I need help with this program. I have done and compiled this program in a single file called bill.cpp. It works fine. But I need to split this program in three files 1. bill.h = contains the class program with methods and variables 2. bill.cpp = contains the functions from class file 3. main.cpp = contains the main program. Please split this program into three files and make the program run. I have posted the code here. #include<iostream>...
I have a question on an assignment for C++. I have my code available here and just want someone to look over it using MS VS 2010 express, for that is what my instructor is using. Write a program that mimics a calculator. The program should take as input two integers and the opreation to be performed. It should then output the numbers, the operator, and the result. (For division, if the denominator is zero, output an appropriate message.) //**This...
Write a program that asks the user to type an even number or 111 to stop. When the user types an even number, display the message “Great Work”, and then ask for input again. When the user types an odd number, display the message “Try again” and ask the user to enter again. When the user enters the sentinel value of 111 the program ends I've attempted this but it went horribly wrong. we are only suppose to use while,...
In the Source Folder (src) of this project create a new C source file called "gcd.c". Once again, copy the contents of the "main.c" file above into the "gcd.c" source file. Modify this program to NOT ask the user to input the second Positive Integer, if the user has entered a program terminating value for the "first" input Postitive Integer. #include <stdlib.h> #include <stdio.h> int main(int argc, char *argv[]) { //============================== setbuf(stdout, NULL); // turns standard output buffering off int...
1. You are given a C file which contains a partially completed program. Follow the instructions contained in comments and complete the required functions. You will be rewriting four functions from HW03 (initializeStrings, printStrings, encryptStrings, decryptStrings) using only pointer operations instead of using array operations. In addition to this, you will be writing two new functions (printReversedString, isValidPassword). You should not be using any array operations in any of functions for this assignment. You may use only the strlen() function...
Please help modify my C program to be able to answer these questions, it seems the spacing and some functions arn't working as planeed. Please do NOT copy and paste other work as the answer, I need my source code to be modified. Source code: #include <stdio.h> #include <string.h> #include <ctype.h> #include <stdlib.h> int main(void) { char title[50]; char col1[50]; char col2[50]; int point[50]; char names[50][50]; printf("Enter a title for the data:\n"); fgets (title, 50, stdin); printf("You entered: %s\n", title);...