1. To set the bit we use bitwise OR operator because 0 | 0 = 1 and 1 | 0 = 1. So for setting a specific bit we use shift operator.
2. To set the bit we use bitwise OR operator because 0 & 0 = 0 and 1 & 0 = 0. So for clearing a specific bit we use shift operator.
3. To toggle the bit we use bitwise XOR operator because 0 ^ 1 = 1 and 1 ^ 1 = 0. So for toggling a specific bit we use shift operator.
Code:
//you can use int instead of unit8_t.
#include<stdio.h>
#include<stdint.h>
//function to set odd bits.
uint8_t set(uint8_t n){
for(uint8_t i=1;i<=8;i+=2){
n = (n | (1 << (i - 1)));
}
return n;
}
//function to clear b3,b4,b5 bits.
uint8_t clear(uint8_t n){
for(uint8_t i=3;i<=5;i++){
n = (n & (~(1 << (i - 1))));
}
return n;
}
//function to toggle even bits.
uint8_t toggle(uint8_t n){
for(uint8_t i=2;i<=8;i+=2){
n = (n ^ (1 << (i - 1)));
}
return n;
}
int main()
{
uint8_t n = 6;
printf("%d with odd bist Set: %d\n", n, set(n));
printf("%d with bits b3,b4,b5 clear: %d\n", n, clear(n));
printf("%d with even bits toggle: %d\n", n, toggle(n));
return 0;
}
Screenshot of code:
Output :
S4.1) Write C code to set all odd bits (b1, 63, b5, b7) in the uint_8...
HCS12 assembly language
E2.21 Write an instruction sequence to toggle the odd-number bits and clear the even-number bits of memory location at $66.
A- Using either “ANDA” or “ORAA” instructions, write an HCS12 code to clear bits 0, 2,5, and 7 of register A and leave all other bits unchanged. B- Using either “ANDB” or “ORAB” instructions, write an HCS12 code to set bits 0, 3, and 6 of register B and leave all other bits unchanged.
Hello, i want to ask you Inventory cost
=3.6(B1+B2)/2+3.75(B2+B3)/2+3.98(B3+B4)/2+4.28(B4+B5)/2+4.2(B5+B6)/2+3.9(B6+B7)/2,
So, how to compute 3.6, 3.75, 3.98, 4.28, 4.2? And why 3.6X(B1+B2)
and divide by 2? Also, i want to ask the objective function for
this problem why is minimize, not maximize? Thank you for your
help!
The Upton Corporation manufactures heavy-duty air compressors for the home and light industrial markets. Upton is presently trying to plan its production and inventory levels for the next six months. Because of seasonal fluctuations...
04. a) Write an 8051 C program to send values 00 through FFH to port Pl. b) Write an 8051 C program to send values -3 to +3 to port P1. c) Write an 8051 C program to toggle all bits of P1 (between 55H and AAH) with some delay d) A door sensor is connected to P1.1 and a buzzer to P1.7. Write an 8051 C program to monitor the sensor and when it opens (0') sound the buzzer...
Write a program in 68K assembly code that adds an odd parity to each ASCII character. Your code must satisfy the following specifications: 1. Define the following 64 characters in the SRC address.SRC:DC.B 'Computing and Software Systems, University of Washington Bothell' 2. Define the 64-byte space.DST:DC.B 64 3. Read each of the 64 characters, (i.e., each byte) into D0, check the number of 1s in it, set 1 to the MSB with "ORI.B #$80, D0" to create an odd parity,...
2. Write a C code with a function to swap the first four bits of an 8-bit unsigned number with the last four bits. The main function should include the variable declaration and initialisation using the appropriate data type, the function call and the display of the result in hexadecimal. The parameter(s) should be passed to the function by reference.
Write a program in C language that lets a user manipulate bits individually in a 4-byte variable. The program should begin with all bits having a value of zero. The program should enter a loop where it prints out the current bit values as a single integer using the twos complement bit model. It should then prompt the user to either set a bit, clear a bit, or exit. If the user desires to set a bit or clear a...
Draw an automaton which will accept all integers (in binary form) of Odd parity (number of 1’s in the string is odd). Now write a code in a high level language (C++) for the automaton. Take inputs of two strings, one of odd parity (e.g, 00111011) and another one of even parity (e.g., 1100110011). Your code should accept the right string and reject the wrong one. Print your code and results
How to write this code in C++???? using fstream Write a program which: Asks the user to enter a positive integer greater than 0 Validates that the entry is a positive integer Outputs the digits in reverse order with a space separating the digits Outputs the even digits not in reverse order with a space separating the digits (consider zero to be even) If there are no even digits, the an appropriate message should be displayed: There are no even...