Click to See Complete Forum and Search --> : Very Simple Question about "Old C"


ltom2000
May 13th, 1999, 12:42 PM
In an "Old C" code, I have:

int Num, reg_id;

Num= (((reg_id/10) % 10) << 4) + (reg_id % 10);

Could you please tell me what "<<" means? Thanks.

mroberts
May 13th, 1999, 12:47 PM
i was not aware that that was supported under c.
it looks like a Redirect.

i would love to know as none of my programs in c ever had that.

Michael Decker
May 13th, 1999, 01:01 PM
Its the left shift operator. The value is being shifted to the left by 4 bits.

Its not old, C and C++ have bit operators.

Paul McKenzie
May 13th, 1999, 01:28 PM
<< and >> are shift left and shift right operators, respecively. C++ has these operators just like C. As a matter of fact, shifts are what these operators are *supposed* to do if not overloaded. The "redirect" operators are really overloaded "shift left" and "shift right" operators. The <iostream> header is responsible for turning a shift operator to a redirect operator. Try to compile a C++ program that uses "cout" without including the iostream header.

I have written C (and C++) programs that have plenty of shift operators in them, mostly for fast multiply and divide by powers of 2, and when testing bits of a variable in a loop like this:

long status = some number;
#include <iostream>
for (long i = 0; i < 32; i++ )
if ( status & (1L<<i) )
{ /* ... Status bit i is ON ... */ }
else
{ /*... status bit i is OFF ... */}

Regards,

Paul McKenzie

Matt Ellison
May 13th, 1999, 01:30 PM
This is a bit wise shift operator. it goes down to the bit level of the object and shifts it by x amount of bits, it is usually used in encryption. the only problem is that i believe you are using a standard (int) if you use bit wise operator's on a common int you usually do not come up with the desired result due to the fact that C stored things in 2's complement. Usually the programmer uses an un-signed int this does not go through the converstion.

ltom2000
May 13th, 1999, 01:54 PM
Thanks for all responses. In the statement

AryChr[5]=(record % 10L) + '0';

where AryChr is an array of char and record is a long int.

What does 10L mean? Why they put '0' there?

Thanks a lot.

Matt Ellison
May 13th, 1999, 02:36 PM
This is where i become very tempted to ask 'What are you working on??? :) ' because i can tell you what that does although i'm not sure what the L stands for...what it does is find the ascii number related to value of record..ie 56 is '8' in ascii..to test this run the program in the debugger and find the numeric value of record..then go to MS Word and hold down the <Alt> key and type in the number ie 56 MS Word will then display '8' which is the ascii representation of that number...i have no idea where you could find the table for this information..hope that helps

Saeed R
May 13th, 1999, 07:19 PM
L is for LONG

Daren Chandisingh
May 14th, 1999, 08:12 AM
AryChr[5]=(record % 10L) + '0';

> What does 10L mean?

The L forces size of 10 to be long. It's sort of shorthand for (long)10.

> Why they put '0' there?

"They" are trying to arrive at a character that is offset from '0' by the result of the previous operation.

For example if (record % 10L) evaluates to 1 then AryChr[5] gets set to 1+'0', or '1'. That is, it is set to the character that is offset from '0' by the amount specified.

As the results can only be in the range 0 to 9 (dues to the %10), this is simply a convenient shorthand for filling the arracy with the character equivalent of the actual result. The alternative would have been:


char s[2];
sprintf (s, "%1ld", (record % 10L));
AryChr[5] = s[0];





--
Daren Chandisingh

akulagin
October 26th, 1999, 03:44 PM
"left Shift"
in your case it means: shft 4 bits to the left.
please, read any C BOOK (for beginners)

alex