## C and C++

Moderators: None (Apply to moderate this forum)
Number of posts: 94611

This Forum Only

Two memory locations M1 and M2, contain 32 bit values. Posted by smilingsheshu on 15 Dec 2008 at 5:30 AM
Two memory locations M1 and M2, contain 32 bit values.
Write a program to compare the 3 bit values ( bit19,bit18,bit17)of M1
with 3 bit values ( bit9,bit8,bit7) of M2 and print EQUAL if they are equal

Re: Two memory locations M1 and M2, contain 32 bit values. Posted by Malcolm_McLean on 15 Dec 2008 at 5:37 AM
: Two memory locations M1 and M2, contain 32 bit values.
: Write a program to compare the 3 bit values ( bit19,bit18,bit17)of M1
: with 3 bit values ( bit9,bit8,bit7) of M2 and print EQUAL if they
: are equal
:
It's a bit of a bad question, because a program cannot compare two memory locations, unless it has access to core memory it doesn't own.

I suspect the teacher means "input two 32 it numbers".

long is guaranteed to be at least 32 bits. So you can use scanf("%ld" ...) to input the numbers.
Then use the and (&) and shift (>>) operators to manipulate your bits, and printf() to output the result.

Re: Two memory locations M1 and M2, contain 32 bit values. Posted by Lundin on 16 Dec 2008 at 12:59 AM
: It's a bit of a bad question, because a program cannot compare two
: memory locations, unless it has access to core memory it doesn't own.

Nobody said "Windows"!
Re: Two memory locations M1 and M2, contain 32 bit values. Posted by Ed Hall on 15 Dec 2008 at 9:49 AM
This seems rather simple on a 32 bit machine and compiler:

Use unsigned int and grab the two values

Create two masks to isolate the important bits

Use bitwise and & to modify the two values

Use bitwise shift << or >> to move one or the other value 10 bits
(Or, you could simply multiply or divide by the appropriate value)

Use simple if() to compare the new values

It should only take about seven actual lines of code.

Take Care,
Ed

## Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic