How to program using const uint8_t* const x?

  • (2 Pages)
  • +
  • 1
  • 2

18 Replies - 1661 Views - Last Post: 15 August 2012 - 12:43 AM Rate Topic: -----

#16 nazzie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 39
  • Joined: 31-July 12

Re: How to program using const uint8_t* const x?

Posted 13 August 2012 - 03:57 AM

View PostSkydiver, on 12 August 2012 - 12:20 AM, said:

If it's complaining about this line:
   uint8_t bcd[] = {0xf64 , 0xf32};



It's because uint8_t is only one byte. 0xf64 is 2 bytes. So is 0xf32. So the compiler is warning you about the truncation because it will likely on store 0x64 and 0x32 respectively.


thanks.

can the codes above be the same as this bcd to binary program?
I assumed the output of the
uint8_t bcd = 0x42;
bcd -= ((bcd & 0xF0) >> 4) * 6;
printf("%d", bcd);





would be the same as this.

int main()
{
int Num;
int A=0,B=0,C,D,E=1;
int F[100];



printf("Enter the number :");
scanf("%d",&Num);


while(Num>0)

{

A=Num%2;

F[B]=A;

Num=Num/2;

B++;

}

if(B<4)

{
       C=B;

for(D=1;D<=4-C;D++)

{

F[B]=0;

B++;
}
}

if(B>4)

{

for(D=0;D<B;D++)

{ if(E==4)

E=1;
else

E++;
}
E--;
if(E<4)
{ for(D=1;D<=4-E;D++)

{
F[B]=0;

B++;

}
}

}

for(E=1,D=B-1;D>=0;D--,E++)

{ printf("%d",F[D]);

if(E==4)

{ printf(" ");

E=0;
}

}

getch();

}

Was This Post Helpful? 0
  • +
  • -

#17 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 1929
  • View blog
  • Posts: 5,743
  • Joined: 05-May 12

Re: How to program using const uint8_t* const x?

Posted 13 August 2012 - 10:04 AM

That horrible piece of code with the the undecipherable variable names shows the individual bits of a Num. It takes a number value and just examines its binary representation bits.

The code with the bit masking and bit shifts, converts a number stored a BCD into a number stored in the now more common binary representation, and then prints out the value.

The objective of the function of your assignment is to return the normal binary representation of a 2 byte BCD number.

Are you sure you are clear in your mind how a BCD number is stored in bits as compared to a binary number?
Was This Post Helpful? 0
  • +
  • -

#18 nazzie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 39
  • Joined: 31-July 12

Re: How to program using const uint8_t* const x?

Posted 14 August 2012 - 09:35 PM

I think so, correct me if im wrong please.

A Binary Coded Decimal has 4 bits per digit from 0 to 9 and is composed of binaries. while a binary number has 0's and 1's, and is is converted from base 2.
so with the function that was given to me I have to have 16 bytes BCD to its corresponding Binary Value.

when you say return the normal binary value is that the same as having an output of a Binary right?

i'm confused in the function return, I thought when you use that it will just be going back to zero or void.
Was This Post Helpful? 0
  • +
  • -

#19 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 1929
  • View blog
  • Posts: 5,743
  • Joined: 05-May 12

Re: How to program using const uint8_t* const x?

Posted 15 August 2012 - 12:43 AM

View Postnazzie, on 14 August 2012 - 09:35 PM, said:

I think so, correct me if im wrong please.

A Binary Coded Decimal has 4 bits per digit from 0 to 9 and is composed of binaries. while a binary number has 0's and 1's, and is is converted from base 2.

That is essentially correct. (Unfortunately, you are doing an apples to oranges comparison between the two. More on this below.)

A Binary Coded Decimal has 4 bits per digit from 0 to 9 and is usually stored 2 digits to a byte. A binary number can be stored in a byte, two bytes, or more depending on the data type.

View Postnazzie, on 14 August 2012 - 09:35 PM, said:

so with the function that was given to me I have to have 16 bytes BCD to its corresponding Binary Value.

No. You have 2 bytes of BCD that gives you 16 bits.

View Postnazzie, on 14 August 2012 - 09:35 PM, said:

when you say return the normal binary value is that the same as having an output of a Binary right?

i'm confused in the function return, I thought when you use that it will just be going back to zero or void.

Unfortunately, the word "binary" is overloaded in the world of computers, and even more so in that function and this is what is leading to the confusion. That function is supposed to take a 4 digit BCD number stored in 2 bytes, and return integer that represents that value.

I'm guess, but I think the other part of the confusion is that you are doing an apples to oranges comparison between BCD and binary values. You are looking at the BCD number as 4 bit groups, while binary as individual bits that happen to be strung together.

You probably understand this, but I'll try to explain the different representations of the same value. Let's take the decimal number 1234. The number can be stored multiple ways. Here's the we are concerned with:
16 bit integer storing 1234:    0000 0100 1101 0010

16 bit BCD number storing 1234: 0001 0010 0011 0100


Your function is suppose to take that second representation, and convert it to the first representation.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2