• Status: Solved
• Priority: Medium
• Security: Public
• Views: 340

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

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
ThePATMAN26
• 3
• 3
• 2
1 Solution

Commented:
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

Commented:
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 Commented:
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

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

Author Commented:
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

Commented:
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

Commented:
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

## Featured Post

• 3
• 3
• 2
Tackle projects and never again get stuck behind a technical roadblock.