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

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
Asked:
ThePATMAN26
  • 3
  • 3
  • 2
1 Solution
 
jkrCommented:
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
 
Hugh McCurdyCommented:
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
 
ThePATMAN26Author 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Hugh McCurdyCommented:
There's an assembly language section here at EE.   Progamming->Programming Languages->Assembly
0
 
ThePATMAN26Author 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
 
jkrCommented:
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
 
Hugh McCurdyCommented:
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

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

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