bit shifting

D

digi.empire

trying to wrap my mind around these two exercises:

1. given an int (which is 16 bits in c), if the 16 bits are:

A15A14A13A12A11A10A9A8A7A6A5A4A3A2A1A0

it will output an int in whose 16 bits are:

A7A6A5A4A3A2A1A0A15A14A13A12A11A10A9A8

2. given an int(which is 16bits in c) if the 16 bits are:

A15A14A13A12A11A10A9A8A7A6A5A4A3A2A1A0

it will perform a cirular right shift 5 so that the new int's 16 bits
are:

A12A11A10A9A8A7A6A5A4A3A2A1A0A15A14A13

thanks in advanced.
 
J

Jens Thoms Toerring

trying to wrap my mind around these two exercises:
1. given an int (which is 16 bits in c),

No, in C an int has *at least* 16 bits, more are allowed (and
nowadays often the default).
if the 16 bits are:
A15A14A13A12A11A10A9A8A7A6A5A4A3A2A1A0

That's not 16 bits in any notation I have seen.
it will output

What's "it"?
an int in whose 16 bits are:
A7A6A5A4A3A2A1A0A15A14A13A12A11A10A9A8

Again, that doesn't look like bits. Does it mean bit positions
(e.g. A7 standing for the 7th bit, if we start conuting at zero)?
In that case this looks like a rotation by 8 positions.
2. given an int(which is 16bits in c) if the 16 bits are:

it will perform a cirular right shift 5 so that the new int's 16 bits
are:
A12A11A10A9A8A7A6A5A4A3A2A1A0A15A14A13

What's your question? And how does it relate to C? Were you asked
to come up with a line of C that converts a (16-bit integer) from
the first to the second form? In that case read about the bitwise
OR operator ('|') plus the left and right shift operators, i.e.
'<<' and '>>' (but which can only be used with unsigned integers).
Here's a hint: what you need is going to look similar to

y = ( x >> S ) | ( x << T );

(but there are also other ways to write the same and you may have
to logically and ('&') x with 0xFFFF before the shifts if an int
has more than 16 bits on your system) where x and y are (unsigned)
integers. All you still need to figure out are what values to use
for S and T for both the exercises. You can be pretty sure that
S + T will be 16;-)

If this is homework (and that's what it looks like) you better
post what you have tried so far and what your problem actually
is...
Regards, Jens
 
B

Barry Schwarz

trying to wrap my mind around these two exercises:

1. given an int (which is 16 bits in c), if the 16 bits are:

An int must be at least 16 bits but there are a lot of systems where
it is larger.
A15A14A13A12A11A10A9A8A7A6A5A4A3A2A1A0

Since bits are always 1 or 0, what do the 38 characters above
represent?


Remove del for email
 
P

Peter Nilsson

Jens said:
... read about the bitwise OR operator ('|') plus the left and right
shift operators, i.e. '<<' and '>>' (but which can only be used
with unsigned integers).

s/can/should/

The bitwise operators can be used on signed integers too, albeit
with twice as many caveats. [Beware integral promotion when
operating on unsigned short and unsigned char.]
 
D

David T. Ashley

trying to wrap my mind around these two exercises:

1. given an int (which is 16 bits in c), if the 16 bits are:

A15A14A13A12A11A10A9A8A7A6A5A4A3A2A1A0

it will output an int in whose 16 bits are:

A7A6A5A4A3A2A1A0A15A14A13A12A11A10A9A8

2. given an int(which is 16bits in c) if the 16 bits are:

A15A14A13A12A11A10A9A8A7A6A5A4A3A2A1A0

it will perform a cirular right shift 5 so that the new int's 16 bits
are:

A12A11A10A9A8A7A6A5A4A3A2A1A0A15A14A13

(arg said:
thanks in advanced.

Can the rest of us get a grade in the class, too?
 
N

Neil

Jens said:
No, in C an int has *at least* 16 bits, more are allowed (and
nowadays often the default).



That's not 16 bits in any notation I have seen.


What's "it"?



Again, that doesn't look like bits. Does it mean bit positions
(e.g. A7 standing for the 7th bit, if we start conuting at zero)?
In that case this looks like a rotation by 8 positions.



What's your question? And how does it relate to C? Were you asked
to come up with a line of C that converts a (16-bit integer) from
the first to the second form? In that case read about the bitwise
OR operator ('|') plus the left and right shift operators, i.e.
'<<' and '>>' (but which can only be used with unsigned integers).
Here's a hint: what you need is going to look similar to

y = ( x >> S ) | ( x << T );

(but there are also other ways to write the same and you may have
to logically and ('&') x with 0xFFFF before the shifts if an int
has more than 16 bits on your system) where x and y are (unsigned)
integers. All you still need to figure out are what values to use
for S and T for both the exercises. You can be pretty sure that
S + T will be 16;-)

If this is homework (and that's what it looks like) you better
post what you have tried so far and what your problem actually
is...
Regards, Jens

If you stare at it long enough
A15,A14,A13,A12,A11,A10,A9,A8,A7,A6,A5,A4,A3,A2,A1,A0
 
S

Sourcerer

Jens Thoms Toerring said:
(e-mail address removed) wrote:

That's not 16 bits in any notation I have seen.

Ax represents a bit. In the above, A15 is the highest value bit, and A0 is the
lowest value bit.

--
"It is easy in the world to live after the world's oppinion; it easy in solitude
to live after our own; but the great man is he who in the midst of the crowd
keeps with perfect sweetness the independence of solitude."
Ralph Waldo Emerson, Self-reliance 1841
http://pinpoint.wordpress.com/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top