Solved

# Change Binary value by a value of 4 using binary or, shift or and functions

Posted on 2011-10-16
315 Views
I'm using C++ to write a program for school. I'm having some trouble with working with binary values. The professor wants us to start with the value 0x7A060 and increment it by four using the binary and, or and shift functions. The final values should be:

7a060
7a064
7a068
7a06c

Any help anyone could provide would be much much much appreciated!
0
Question by:ThePATMAN26

LVL 86

Accepted Solution

It might just help you to take a look at the binary representation of the last digit of the original and incremented values, which are

0000
0100 // 'or' to add the bit
1000 // 'and' to remove it again, 'or' to add the other
1100 // should be clear

Oh, and sorry, but providing code for this asignment would violate EE's rues, since it is that straightfoward...

0

LVL 13

Expert Comment

PATMAN, thanks for being honest and saying you have a homework assignment.

jkr is correct that we shouldn't provide code.

However, we can look at code that you write and post.  You can also tell us what you are thinking to get guidance.  (Show us that you've attempted the homework and give us something to react to.)
0

Author Closing Comment

Evening,

Thanks to both of you. JKR's comments were just the amount of guidance I needed, I was able to draw up my own source code.

Would either of you have any recommendations on where would be a good place to find some supplemental guidance when it comes to working with assembly level stuff?

Thanks,
-Patrick
0

LVL 13

Expert Comment

There's an assembly language section here at EE.   Progamming->Programming Languages->Assembly
0

Author Comment

Great, one last question. I have the stored value of eleven (1011) and I want to increment it by 1 using the OR function with value 0001. How would you recommend going about it (steps only, I can generate my own code).
0

LVL 86

Expert Comment

Well, you know the result - it's 1100. So, only 'or' will not be sufficiant, you need to mask out the lower two bits with 'and' first, then you can use 'or' to set the 3rd.
0

LVL 13

Expert Comment

Sounds to me like you should create a generic function.  Are you allowed to have a carry bit?

Just like grade school adding except in binary.  Start from the right.  XOR the current bit column to get the bit value in the sum row.  AND the bits to see if you should set the carry bit.  It gets a bit trickier in the 2nd column once the carry bit is on but I bet you can figure it out.

If you can write that, you can add any two integers and you'd be close to how hardware does this work.  (Except you are in serial and hardware does work in parallel.)
0

LVL 86

Expert Comment

0

## Featured Post

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

#### Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!