Not sure if this can be done or not, my bitwise math is not what it should be.
I am trying to combine two int's into 1 byte so it can be shifted out.
Say, the number 3 and 5. I don't want to shift out "B00001000" but shift out "B00110101"
Thanks.
Not sure if this can be done or not, my bitwise math is not what it should be.
I am trying to combine two int's into 1 byte so it can be shifted out.
Say, the number 3 and 5. I don't want to shift out "B00001000" but shift out "B00110101"
Thanks.
So if
3 is B00000011
and
5 is B00000101
what do you expect as the combination?
"B00110101" as shown above in the original post is the combo I want.
The first 4 bits being the three "0011" and the last 4 bits being the five "0101"
The first four (most significant) bits are 16 times bigger than the last 4 (least significant) bits.
To put your 3 into the higher bits, multiply it by 16 = 48, then add your 5 giving 53 = B00110101.
look up binary coded decimal - I think that's what you want to do.
yodabyte:
Not sure if this can be done or not, my bitwise math is not what it should be.
I am trying to combine two int's into 1 byte so it can be shifted out.
Say, the number 3 and 5. I don't want to shift out "B00001000" but shift out "B00110101"Thanks.
Your terminology is wrong. You want to convert two NYBBLES into one BYTE.
You could do this:
// note: lower and upper can only be 0...15, no larger
uint8_t lower_part = 5;
uint8_t upper_part = 3;
uint8_t result = 0; // accumulator to build result
// we mask off any high bits with "& 0b00001111"
result |= (lower_part & 0b00001111); // insert lower nybble
result |= ((upper_part & 0x00001111) << 4); // slide upper part up 4 places and OR it in
// "print" result = 0b 0011 0101
Sleepydoc:
look up binary coded decimal - I think that's what you want to do.
Oh... if he's doing BCD stuff, he may need these:
// convert decimal to BCD
uint8_t dec2bcd (uint8_t dec)
{
return (dec / 10 * 16) + (dec % 10);
}
// convert BCD to decimal
uint8_t bcd2dec (uint8_t bcd)
{
return (bcd / 16 * 10) + (bcd % 16);
}
I guess I was thinking it was harder than it needed to be!!
The multiply by 16 and add the second "int" was exactly what I needed to do.
I am using the MC14489 LED driver chip.
I needed to shift out six nibbles in three separate bytes.
Works perfect now
Thanks to all that replied.
yodabyte:
I guess I was thinking it was harder than it needed to be!!
The multiply by 16 and add the second "int" was exactly what I needed to do.
I am using the MC14489 LED driver chip.
I needed to shift out six nibbles in three separate bytes.
Works perfect nowThanks to all that replied.
Multiplying by 16 is the same thing as a 4 bit left shift, but generally it's better to use the left shift operator because it's almost self documenting. It's completely obvious why you're shifting left 4 bits, but "times 16" means little to nothing without a comment (which you should use liberally in your code anyway - comments that is).
I don't know, but I would guess that a shift is faster (code-wise) than a multiply (unless the compiler is smart enough to know that X16 is the same as <<4).
If someone else reads your code (or even if you go back to it 6 months or a year later) you may ask "why the heck did I multiply by 16?" But "<< 4" is completely obvious.
My 2 cents......
Krupski:
Multiplying by 16 is the same thing as a 4 bit left shift, but generally it's better to use the left shift operator because it's almost self documenting. It's completely obvious why you're shifting left 4 bits, but "times 16" means little to nothing without a comment (which you should use liberally in your code anyway - comments that is).I don't know, but I would guess that a shift is faster (code-wise) than a multiply (unless the compiler is smart enough to know that X16 is the same as <<4).
If someone else reads your code (or even if you go back to it 6 months or a year later) you may ask "why the heck did I multiply by 16?" But "<< 4" is completely obvious.
My 2 cents......
Yes, but one has to take into account the probable level of understanding of the OP. The effect of *16 is obvious to almost anyone. <<4 is not so obvious and more difficult for a newbie to implement.