answersLogoWhite

0


Best Answer

A = A xor B

B = A xor B

A = A xor B

in C...

A^=B;

B^=A;

A^=B;

User Avatar

Wiki User

14y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How can you devise a way to interchange two variables A and B without using a third variable or register using Exclusive OR?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

Difference between register variable and automatic variables?

Register variables are stored in register of microprocessor/micro-controller. The read/write access to register variable is the fastest because CPU never need any memory BUS operation to access these variable. Auto variable are stored in stack thus access are much slower. Auto variable can be converted to register by using register keyword before it. It has platform specific limitation. Register variable will work only if free registers are available to hold the variable for a function scope. In case of Microprocessor or microcontrollers having very less number of general purpose registers will never take register variable even if we declare it as register.


What are the different storage class in c?

The four storage classes in C are: automatic, static, external and register. Note that storage classes are not classes in the object-oriented programming sense, they simply define the scope (visibility) of a variable.Automatic Variables (auto)All local variables are automatic by default so we seldom see the auto keyword in code. Local variables are variables declared at function scope.Static Variables (static)All global variables are static by default. Global variables are variables declared at file scope (outside of any function). Static variables can also be explicitly declared inside functions to override the default automatic storage class. All static variables, whether global or local, are allocated within the program's data segment (static memory) and do not fall from scope even if declared locally. All static variables are initialised to zero by default.It's best to avoid the use of global variables unless they are declared constant (const) as it can be difficult to keep track of all the places where a global variable is being operated upon (accessed or assigned to). This can lead to data races in multi-threaded applications unless we take steps to synchronise all access and assignment operations upon the variable. For that reason it's best to keep variables as localised as possible, passing arguments into functions whenever we need to cross scopes. Non-constant global variables should really only be considered if they truly represent a global concept within the file in which they are declared.External Variables (extern)External storage can only be applied to a global variable declared outwith file scope. That is, when a global variable is declared in one file, any external file can gain access to that same global variable simply by declaring the same name and type but with external storage. It follows that external variables are also static variables and is the only case where a variable has two storage classes. Note the local static variables (including local constant variables) cannot be declared external, they are local to the function in which they are declared.This is another reason why it is best to avoid using too many global variables. While we can generally keep track of which code can access a global variable at file scope we have no means of limiting access from outwith that file. Again, prefer local variables to global variables whenever possible.Register Variables (register)A register variable is a variable that we wish to allocate to a CPU register rather than in RAM. Register variables must be no larger than the word-length of the machine and should only be used when we explicitly require fast access to the variable, such as loop counters, accumulators and pointer variables. Note that CPU registers have no address (no identity we can refer to) so we cannot use the unary '&' operator to take the address of a register variable. This means we cannot use pointers to refer to them indirectly which, in turn, means we can only pass them to functions by value (not by reference). However, to do so would defeat the purpose of using the register storage class.Given the limited number of registers available, there is no guarantee that a register variable will actually be allocated to a register; the register keyword is merely a hint to the compiler. It should be noted that modern compilers are extremely good at optimising code so there is seldom any need to explicitly declare register variables.


Where does global variables stored in C?

It depends entirely on what platform you are using. In an embedded environment, for instance global/static variables go into different RAM memory segments depending on whether or not they are initialised. constants are often left in ROM automatic variables are normally placed of the stack of the currently running task but not always.


What is the difference between auto storage class and register storage class?

register behaves like auto class but auto variables are actually local variables.They are created when the functions are created but they are destroyed when the fns is exited.But register-variables are used to speed up the operations


Advantages of register variable?

Register variables are a special case of automatic variables. Automatic variables are allocated storage in the memory of the computer; however, for most computers, accessing data in memory is considerably slower than processing in the CPU. These computers often have small amounts of storage within the CPU itself where data can be stored and accessed quickly. These storage cells are called registers. Normally, the compiler determines what data is to be stored in the registers of the CPU at what times. However, the C language provides the storage class register so that the programmer can ``suggest'' to the compiler that particular automatic variables should be allocated to CPU registers, if possible. Thus, register variables provide a certain control over efficiency of program execution. Variables which are used repeatedly or whose access times are critical, may be declared to be of storage class register. Also these register variables are used in huge projects the tiny program developers are not interested to include these register variables, because the tiny programs never requires more time complete its job. These register variables may be used to store constant values so as to make use of it anywhere in the programs. main{ register float a=0;}

Related questions

What are register variables What are the advantage of using register variables?

Asks the compiler to devote a processor register to this variable in order to speed the program's execution. The compiler may not comply and the variable looses it contents and identity when the function it which it is defined terminates.


What are register variables?

A register variables is that which got space in CPU internal register sets this requst can be granted or rejected by CPU ,by register veriable the processing speed become much faster for that variable.


What are register variables in c?

A register specifier for a variable is a compiler hint that the variable is used often and should be kept in a CPU register. The compiler will treat the variable as automatic, but it will give it preference for staying in a register between sequence points.


What is an alternating function?

An alternating function is a function in which the interchange of two independent variables changes the sign of the dependent variable.


What is a register variable in c programming?

A register variable is a variable that we wish to allocate in a CPU register rather than in a memory address (RAM). Register variables must be less than or equal in length to the word length of a CPU register which typically limits their use to integral types and pointers. Typical uses for register variables include counters, accumulators and other regularly-accessed variables within a localised code block. Storage classes were originally inherited from B, however only the static and extern storage classes have any meaningful purpose in C. The register storage class is largely redundant while the auto storage class is wholly redundant. Explicitly declaring a register variable is no guarantee the variable will actually be allocated to a register, it is merely a hint to the compiler that the variable is a candidate. However, modern compilers are perfectly capable of determining which variables make good candidates without hints, so there is typically no need to explicitly declare them. If we have more good candidates than there are registers available, explicitly declaring our preferred candidates might be considered worthwhile, however it's generally better to just let the compiler decide. Note that register variables have no memory address. If we take the address of any variable using the unary '&' operator, then that variable can no longer be considered as a register variable candidate by the compiler. If we explicitly declare a register variable and subsequently attempt to take its address, the compiler will issue a warning, yet another reason to simply let the compiler decide.


Difference between register variable and automatic variables?

Register variables are stored in register of microprocessor/micro-controller. The read/write access to register variable is the fastest because CPU never need any memory BUS operation to access these variable. Auto variable are stored in stack thus access are much slower. Auto variable can be converted to register by using register keyword before it. It has platform specific limitation. Register variable will work only if free registers are available to hold the variable for a function scope. In case of Microprocessor or microcontrollers having very less number of general purpose registers will never take register variable even if we declare it as register.


What is the difference between normal variable and register variable in c?

In C/C++ when we declare a variable; e.g int var; for this variable (i.e. var) memory is being reserved in RAM (i.e out side processor). If we declare variable like that; register int var2; for this variable memory is being reserved in register of CPU (i.e. withing processor) But register variables are discouraged because processor has to work with registers..... Note: strictly speaking, storage class 'register' means: dear compiler, you might optimize this variable into register, as I won't ever request its address. But of course, it's up to you to decide.


How do the test variables (independent variables) and outcome variables (dependent variables) in an experiment compare?

The test variable (independent variable) controls the outcome variable (dependent variable).


Obtain algorithm that corresponds a program for the interchange of two numerical variable values?

In general, to swap two variables A and B with each other, you need a third variable T of the same type. You then perform the sequence T = A, A = B, and B = T. For the special case where A and B are binary types, you can swap them without a temporary variable using a bit-wise exclusive or operator. Using C/C++ syntax, you would use the sequence A ^= B, B ^= A, and A ^= B. For this to succeed, the exclusive or operator (^) must be bitwise. Actually, it is also possible to swap any two arbitrarily typed variables, such as float or object, so long as you can properly do the bitwise exclusive or operation, perhaps by doing typcasts or by playing games with pointers - although any of that is potentially dangerous and not always portable.


What are the different storage class in c?

The four storage classes in C are: automatic, static, external and register. Note that storage classes are not classes in the object-oriented programming sense, they simply define the scope (visibility) of a variable.Automatic Variables (auto)All local variables are automatic by default so we seldom see the auto keyword in code. Local variables are variables declared at function scope.Static Variables (static)All global variables are static by default. Global variables are variables declared at file scope (outside of any function). Static variables can also be explicitly declared inside functions to override the default automatic storage class. All static variables, whether global or local, are allocated within the program's data segment (static memory) and do not fall from scope even if declared locally. All static variables are initialised to zero by default.It's best to avoid the use of global variables unless they are declared constant (const) as it can be difficult to keep track of all the places where a global variable is being operated upon (accessed or assigned to). This can lead to data races in multi-threaded applications unless we take steps to synchronise all access and assignment operations upon the variable. For that reason it's best to keep variables as localised as possible, passing arguments into functions whenever we need to cross scopes. Non-constant global variables should really only be considered if they truly represent a global concept within the file in which they are declared.External Variables (extern)External storage can only be applied to a global variable declared outwith file scope. That is, when a global variable is declared in one file, any external file can gain access to that same global variable simply by declaring the same name and type but with external storage. It follows that external variables are also static variables and is the only case where a variable has two storage classes. Note the local static variables (including local constant variables) cannot be declared external, they are local to the function in which they are declared.This is another reason why it is best to avoid using too many global variables. While we can generally keep track of which code can access a global variable at file scope we have no means of limiting access from outwith that file. Again, prefer local variables to global variables whenever possible.Register Variables (register)A register variable is a variable that we wish to allocate to a CPU register rather than in RAM. Register variables must be no larger than the word-length of the machine and should only be used when we explicitly require fast access to the variable, such as loop counters, accumulators and pointer variables. Note that CPU registers have no address (no identity we can refer to) so we cannot use the unary '&' operator to take the address of a register variable. This means we cannot use pointers to refer to them indirectly which, in turn, means we can only pass them to functions by value (not by reference). However, to do so would defeat the purpose of using the register storage class.Given the limited number of registers available, there is no guarantee that a register variable will actually be allocated to a register; the register keyword is merely a hint to the compiler. It should be noted that modern compilers are extremely good at optimising code so there is seldom any need to explicitly declare register variables.


What are the 3 kinds of variables?

independent variable,depedent variable and control variable are the 3 kinds of variables.


Variables that do not change in a experiment?

the dependant variable