473,396 Members | 1,768 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

bit shifting

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.

Dec 14 '06 #1
6 2864
di*********@gmail.com wrote:
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:
A15A14A13A12A11A10A9A8A7A6A5A4A3A2A1A0
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
--
\ Jens Thoms Toerring __ _ jt@toerring.de
\__________________________ http://toerring.de
Dec 14 '06 #2
On 13 Dec 2006 17:44:08 -0800, di*********@gmail.com wrote:
>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
Dec 14 '06 #3
Jens Thoms Toerring wrote:
... 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.]

--
Peter

Dec 14 '06 #4
<di*********@gmail.comwrote in message
news:11*********************@79g2000cws.googlegrou ps.com...
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
(arg << 8) | ((arg >>8) & 0xFF) /* Mask necessary due to right shift
possible sign propagation. */
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 << 3) | (arg >13 & 0x7)
thanks in advanced.
Can the rest of us get a grade in the class, too?

Dec 14 '06 #5
Jens Thoms Toerring wrote:
di*********@gmail.com wrote:
>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:
>A15A14A13A12A11A10A9A8A7A6A5A4A3A2A1A0
>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
--
\ Jens Thoms Toerring __ _ jt@toerring.de
\__________________________ http://toerring.de
If you stare at it long enough
A15,A14,A13,A12,A11,A10,A9,A8,A7,A6,A5,A4,A3,A2,A1 ,A0
Dec 14 '06 #6
"Jens Thoms Toerring" <jt@toerring.dewrote in message
news:4u*************@mid.uni-berlin.de...
di*********@gmail.com wrote:
<snip>
>A15A14A13A12A11A10A9A8A7A6A5A4A3A2A1A0

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/

Dec 14 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: David Stockwell | last post by:
Hi, My background is c/c++ and java. I'm learning python at this point. My question is does python share java's peculiar mode of bit shifting, or does python adhere closer to c's bit shifting?...
17
by: Jack | last post by:
Hi, This is a strange problem I am encountering. I have a asp page with a confirmation.asp page that saves data to a table. There are few text fields that are captured by the confirmation page as...
9
by: GGG | last post by:
Noticed something odd in the way bit shifting was working today. As far as I have ever heard, shifting will shift in zeros(signed ints aside) However I foudn something odd when I am shifting...
8
by: ben | last post by:
i have a bit of code, that works absolutely fine as is, but seems over complicated/long winded. is there anyway to shorten/simplify it? the code is below. description of it: it's like strcpy in...
2
by: salsipius | last post by:
Can someone please help me clarify the below code. I think the shifting has to do with converting datatypes and/or loss of data but am not really clear on the details, could you help shed some...
10
by: krunalb | last post by:
Hi, I am trying to shift unsigned long long value by 64 bits and this is what i get #include <stdio.h> int main() { unsigned short shiftby= 64;
20
by: Charles Sullivan | last post by:
I understand different processor hardware may store the bits in a byte in different order. Does it make a difference in C insofar as bit-shifting unsigned char variables is concerned? E.g, if I...
16
by: lak | last post by:
i know left and right shift normally,but i cant know what happens if it is negative. for example int x=-2; x<<=1;//what happens here
4
by: Neil | last post by:
I previously posted about data shifting between records in my Access 2000 MDB with a SQL Server 7 back end, using ODBC linked tables. Every once in a while, data from one record mysteriously...
12
by: Boltar | last post by:
I seem to be having yet more wierd issue with bit shifting. It seems the following code doesnt do anything under gcc (ie it returns -1 as both results). Anyone know why? Is it another language...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.