Sunday, November 1, 2020

C programming - Count number of bits to be flipped to convert A to B

From :- https://aticleworld.com/interview-questions-on-bitwise-operators-in-c/

In this question, you need to count flipped bits that require to convert A to B. To accomplish this task you need to find the number of bits that are different in A and B.

Suppose, A = 8, B = 7
Binary representation of A => 00001000
Binary representation of B => 00000111
Here we have to flip highlighted four bits of A
to make it B.

 Algorithm

  1. Calculate XOR of A and B.With the help of XOR, we will discard the common bits and set the bits that are different in numbers A and B.
  2. Count the set bits of the above calculated XOR result.

Example code,

#include <stdio.h>
//function to calculate flipped bits
int CountFlippedBits(int A, int B)
{
int XorResult = 0;
int count = 0;
//Doing Ex-or
XorResult = (A ^ B);
//Count set bits
while (XorResult)
{
count += XorResult & 1;
XorResult >>= 1;
}
return count;
}
int main()
{
int A = 8;
int B = 7;
int ret = 0;
//Function return count of flipped bits
ret = CountFlippedBits(A,B);
printf("Flipped Bits = %d\n",ret);
return 0;
}

C programming - Swap all odd and even bits

From  - https://aticleworld.com/interview-questions-on-bitwise-operators-in-c/ 

you need to swap the even and odd bits. To accomplish the above task you need to first find the even and odd bits then shift these bits. See the below steps,

Let the input number is data (Assuming integer size is 4 bytes),

  1. Get all even bits of data by doing bitwise and (&) of data with 0xAAAAAAAA (data & 0xAAAAAAAA).
  2. Get all odd bits of data by doing bitwise and (&) of data with 0x55555555 (data & 0x55555555).
  3. Right shift all even bits ((data & 0xAAAAAAAA)>>1).
  4. Left shift all odd bits ((data & 0x55555555)<<1).
  5. Combine the value which gets from the left and right operation ((data & 0xAAAAAAAA)>>1 | (data & 0x55555555)<<1).

Example code,

#include <stdio.h>
int main()
{
int data = 2;
data = ((data & 0xAAAAAAAA)>>1 | (data & 0x55555555)<<1);
printf("%d",data);
return 0;
}

Saturday, September 5, 2020

ARM Exceptions - Part 1

What are Exceptions ?

An exception is any condition that requires the core to halt normal execution and instead execute a dedicated software routine known as an exception handler associated with each exception type.Exceptions are conditions or system events that usually requires remedial action or an update of  system status by privileged software to ensure smooth functioning of the system. This is called handling an exception. When the exception has been handled, privileged software prepares the core to resume whatever it was doing before taking the exception. Exceptions are normally synchronous in nature however the occurrence of IRQ and FIQ interrupts are not directly related to the software being executed by the core at any given time, they are classified as asynchronous exceptions.

Aborts can be generated either on failed instruction fetches (prefetch aborts) or failed data accesses (data aborts). They can come from the external memory system giving an error response on a memory access (indicating perhaps that the specified address does not correspond to real memory in the system).

Alternatively, the abort can be generated by the Memory Management Unit(MMU) of the core. An operating system can use MMU aborts to dynamically allocate memory to applications.

Asynchronous(imprecise) and Synchronous(precise) Abort 

An Abort is described as synchronous if it is generated as a result of execution or attempted execution of the instruction stream, and where the return address will provide details of the instruction that caused it. Aborts generated by the MMU are always synchronous in nature.

An Asynchronous abort is NOT generated by executing instructions, while the return address might not always provide details of what caused the abort. The occurrence of IRQ and FIQ interrupts are not directly related to the software being executed by the core at any given time, they are classified as asynchronous exceptions.

Synchronous exceptions can occur for a number of possible reasons:

• Aborts from the MMU. For example, permission failures or memory areas marked as Access flag fault.

• SP and PC alignment checking.

• Unallocated instructions.

• Service Calls (SVCs, SMCs and HVCs).

Exception generating instructions

Execution of certain instructions can generate exceptions. Such instructions are typically executed to request a service from software that runs at a higher

Privilege level

• The Supervisor Call (SVC) instruction enables User mode programs to request an Operating System service.

• The Hypervisor Call (HVC) instruction, available if the Virtualization Extensions are implemented, enables the guest OS to request Hypervisor services.

• The Secure Monitor Call (SMC) instruction, available if the Security Extensions are implemented, enables the Normal world to request Secure world services.