A

@Adarsh222

toggle the bits in binary

C
2 years ago
#include <stdio.h> void printBinary(int val, int noBits){ for(int i=noBits-1; i>=0; i--){ printf("%d",(val>>i)&1); } printf("\n"); } int main() {

function pointer basics

C
2 years ago
#include <stdio.h> //typedef int (fp)(int, int)//(fp) brackets are imp int add(int a, int b) { int sum = a + b; return sum; // You need to return the sum from the function. } int main() { int (*fp)(int, int); // Declare a function pointer that points to a function taking two ints and returning int.

Store the binary & hex values

C
2 years ago
#include <stdio.h> int main() { int a = 0b101010; char binaryRepresentation[7]; // 6 bits + 1 for null terminator // Fill the binaryRepresentation array with binary digits for (int i = 5; i >= 0; i--) { binaryRepresentation[5 - i] = ((a >> i) & 1) + '0'; // Convert to ASCII }

count the no of 1s in hex and binary

C
2 years ago
#include <stdio.h> int main(){ int bin = 0b10101010; int hex = 0xA10;//1010 0001 0000 //A hexadecimal digit represents values from 0 to 15, or 0000 to 1111 in binary. //In other words, one hexadecimal digit maps to 4 binary digits (bits). int c = 0;

bitwise binary startbit to stopbit get middle value

C
2 years ago
#include <stdio.h> int main() { int bin = 0b01011110; int x = 8; // Assuming an 8-bit binary representation int start_index, end_index; scanf("%d %d", &start_index, &end_index); for (int i = end_index; i >= start_index; i--) {

bitwise binary get the digits

C
2 years ago
#include <stdio.h> int main() { int bin = 0b0101; int x = 0; scanf("%d",&x); for(int i=0;i<x;i++){ int it = x-i-1; int res = (bin>>it) & 0b1;

Binary bitwise vs hex get values from per no

C
2 years ago
#include <stdio.h> int main() { int val = 0xA54B; // Assuming 16-bit integer // User input to specify which digit to extract (1 to 4) int userInput; printf("Enter a digit (1 to 4): \n"); scanf("%d", &userInput);

Binary bitwise vs hex get particular element imp

C
2 years ago
#include <stdio.h> int main() { int bin = 0b0101; //4321 int hex = 0x5A04; //12 8 4 null -> since each element like 5 = 0101, A = 1010 so it take 4 values each // Extracting the third bit (index 2) int thirdBit = (bin >> 2) & 1; int trdb = (hex >> 8) & 0xF;

hex get the bits

C
2 years ago
#include <stdio.h> #include <stdint.h> uint32_t gethex(uint32_t var, int index, int count) { uint32_t rev = 0; for (int i = 0; i < count; i++) { rev = (rev << 4) | ((var >> (4 * (index + i))) & 0xF); } return rev; }

Binary bitwise imp

C
2 years ago
#include <stdio.h> // Function to print binary representation of an integer void printBinary(int num) { if (num == 0) { printf("0"); return; } int binary[32]; // Assuming 32-bit integers

mem related all fun

C
2 years ago
memcpy: Copies a specified number of bytes from one memory location to another. void *memcpy(void *dest, const void *src, size_t n); memmove: Similar to memcpy, but handles overlapping memory regions correctly. void *memmove(void *dest, const void *src, size_t n); memset: Sets a specified number of bytes in a memory region to a given value.

memmove imp pgm

C
2 years ago
#include <stdio.h> #include <stdint.h> #include <string.h> int main() { char str[10] = "adarsh"; char new[5] = "Hi"; char sub[10] = {0};

Bitwise imp tricks

C
2 years ago
#include <stdio.h> a = a ^ b; b = a ^ b; a = a ^ b; Check if a Number is Even or Odd: if (num & 1) { // odd } else {

memmove string

C
2 years ago
#include <stdio.h> #include <string.h> int main() { char str[4] = "SAM"; char val[80] = "SAMhelloSAMyelloSAM12345SAM54321helloSAMyelloMAShello"; int len = strlen(val); char *index = strstr(val, str); if (index != NULL) {

Upper to lower covert imp

C
2 years ago
#include <stdio.h> #include <string.h> int main() { char ip[15] = "SaMsuNG Exynos"; int len = strlen(ip); for (int i = 0; i < len; i++) { if (ip[i] >= 'A' && ip[i] <= 'Z') {//A-Z = 65-90 // a-z = 97-122 // Convert alternate uppercase letters to lowercase

Reverse the hexadecimal value

C
2 years ago
#include <stdio.h> #include <stdint.h> int main() { uint32_t var = 0x12345678;//its 32bits(4bytes) -> hexadecimal 1no = 4bits; uint32_t rev = 0; for (int i = 0; i < 8; i++) {

questions

C
2 years ago
int main() { uint32_t var = 0x12345678; uint32_t rev = 0; int count++; //rev = (var & 0xF) >> 32; //rev = (var & 0x0F) >> 28; //0x00F

Both Hex and binary are same with bitwise IMP

C
2 years ago
#include <stdio.h> int main() { int a = 0xA;//1010 int b = 10; a = a & (1<<1); /*b = b ^ (1<<0); b = b ^ (1<<2);*/ //or b = b ^ (1<<0) ^ (1<<2);

Bit is set or not

C
2 years ago
#include <stdio.h> int main() { unsigned int var = 2; //0010 if var = 8 1000 then bit is set if((var & (1<<3)) == 0){ // 0010 & ((0001 << 3)=(1000)) // 0010 & (1000) == 0 printf("bit is not set"); } else{ printf("bit is set");

Extract individual digits

C
2 years ago
#include <stdio.h> #if 0 int main() { int val = 0xA54B;//by default its uint16_t!! // Extract individual digits int digit1 = val & 0x000F; // so it takes only F bit and extract that data int digit2 = (val & 0x00F0) >> 4;// takes 4 value but push it to last 000F like value 4