Solved

how do I remove the MSB of a value before concatenating it with another?

Posted on 2012-03-16
2
383 Views
Last Modified: 2012-03-16
I am writing to two 7-segment LEDs and each of them are controlled with seven bits. I have each digit I want to display stored as a byte. I need to write the high and low digit values as a concatenation to the 7-segment LEDs.  This is a problem because the MSB of the low digit will become the LSB of the high digit. For example, if I am writing the value 99 to the 7-segment LEDs, "0011000" needs to be written to each 7-segment LED.  But because the values are stored as bytes, "00011000" will be the output value for 9, and the concatentation of 9 and 9 would be 00011000 00011000, and "0110000 0011000" will be written to the LEDs instead of "0011000 0011000". How do I get rid of the low digit MSB before concatenating??
0
Comment
Question by:frostyourself
2 Comments
 
LVL 16

Accepted Solution

by:
HooKooDooKu earned 500 total points
Comment Utility
WORD DoubleDigit;
BYTE LowDigit;
BYTE HighDigit;
... /* set your value for the two digits */
DoubleDigit = (WORD) HighDigit << 8 + (WORD) LowDigit << 1;
DoubleDigit = DoubleDigit >> 1;

Open in new window


Basically, shift the high digit to the 2nd byte, then shift the low digit once.  Then shift both together to the right one.

Now I might have your digits transposed here (i.e. you might need to swap HighDigit with LowDigit). but the idea is still the same.  Utilize the << and >> bit shift operators.  You can first utilize the bit shift on a single byte, then concatenate the two bytes (placing one in the high order byte of a 16-bit word) and then bit shift the whole 16-bit value to get the final value you need for the LED display.
0
 

Author Closing Comment

by:frostyourself
Comment Utility
Thank you!
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now