ExamplesConsider n = 20(00010100), k = 3So, the result after turning off the 3rd bit => 00010000 & (1
ExampleFor example consider n = 20(00010100), k = 4. So result after turning on 4th bit => 00010000 | (1
ExampleConsider n = 20(00010100), k = 3 The result after turning off the 3rd bit => 00010000 & ^(1 16sApproach to solve this problemStep 1 − Define a method, where n and k would be the arguments, return type is int.Step 2 − Perform AND operation with n & ^(1
Example − In the given tree, the root node is 1, the root of its left sub tree is 2, and the root of its right sub tree is 3, ... so on.Preorder Tree Traversal Output: 1, 2, 4, 5, 3, 6, 7.Approach to solve this problemStep 1 − First, we’ll define the node structure.Step 2 − In the main method, we would create the above tree.Step 3 − Finally, we will perform the Preorder Tree Traversal.Example Live Demopackage main import "fmt" type Node struct { data int left *Node right *Node } func (root *Node)PreOrderTraversal(){ if ... Read More
The C library function size_t strlen(const char *str) computes the length of the string str up to, but not including the terminating null character.An array of characters is called a string.DeclarationGiven below is the declaration of an array −char stringname [size];For example − char a[50]; string of length 50 charactersInitializationUsing single character constant −char a[10] = { ‘H’, ‘e’, ‘l’, ‘l’, ‘o’ ,‘\0’}Using string constants −char a[10] = "Hello":;Accessing − There is a control string "%s" used for accessing the string till it encounters ‘\0’The strlen ( ) functionThis function gives the length of the string, i.e., the number of ... Read More
Pass by reference in C programming language is the addresses which are sent as arguments.AlgorithmAn algorithm is given below to explain the working of pass by value in C language.START Step 1: Declare a function with pointer variables that to be called. Step 2: Declare variables a, b. Step 3: Enter two variables a, b at runtime. Step 4: Calling function with pass by reference. jump to step 6 Step 5: Print the result values a, b. Step 6: Called function swap having address as arguments. i. Declare temp variable ii. Temp=*a iii. *a=*b iv. *b=temp ... Read More
Pass by value is termed as the values which are sent as arguments in C programming language.AlgorithmAn algorithm is given below to explain the working of pass by value in C language.START Step 1: Declare a function that to be called. Step 2: Declare variables. Step 3: Enter two variables a, b at runtime. Step 4: calling function jump to step 6. Step 5: Print the result values a, b. Step 6: Called function swap. i. Declare temp variable ii. Temp=a iii. a=b iv. b=temp STOPExampleGiven below is the C program to swap the two numbers ... Read More
The C library memory allocation function void *realloc(void *ptr, size_t size) attempts to resize the memory block pointed to by ptr that was previously allocated with a call to malloc or calloc.Memory allocation FunctionsMemory can be allocated in two ways as explained below −Once memory is allocated at compile time, it cannot be changed during execution. There will be a problem of either insufficiency or else wastage of memory.The solution is to create memory dynamically i.e. as per the requirement of the user during execution of program.The standard library functions which are used for dynamic memory management are as follows ... Read More
The C library memory allocation function void *calloc(size_t nitems, size_t size) allocates the requested memory and returns a pointer to it.The difference in malloc and calloc is that malloc does not set the memory to zero, whereas, calloc sets the allocated memory to zero.Memory allocation FunctionsMemory can be allocated in two ways as explained below −Once memory is allocated at compile time, it cannot be changed during execution. There will be a problem of either insufficiency or else wastage of memory.The solution is to create memory dynamically i.e. as per the requirement of the user during execution of program.The standard ... Read More
The C library memory allocation function void *malloc(size_t size) allocates the requested memory and returns a pointer to it.Memory allocation FunctionsMemory can be allocated in two ways as explained below −Once memory is allocated at compile time, it cannot be changed during execution. There will be a problem of either insufficiency or else wastage of memory.The solution is to create memory dynamically i.e. as per the requirement of the user during execution of program.The standard library functions which are used for dynamic memory management are as follows −malloc ( )calloc ( )realloc ( )free ( )The Malloc() FunctionThis function is ... Read More