473,625 Members | 2,733 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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:

A15A14A13A12A11 A10A9A8A7A6A5A4 A3A2A1A0

it will output an int in whose 16 bits are:

A7A6A5A4A3A2A1A 0A15A14A13A12A1 1A10A9A8

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

A15A14A13A12A11 A10A9A8A7A6A5A4 A3A2A1A0

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

A12A11A10A9A8A7 A6A5A4A3A2A1A0A 15A14A13

thanks in advanced.

Dec 14 '06 #1
6 2882
di*********@gma il.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:
A15A14A13A12A11 A10A9A8A7A6A5A4 A3A2A1A0
That's not 16 bits in any notation I have seen.
it will output
What's "it"?
an int in whose 16 bits are:
A7A6A5A4A3A2A1A 0A15A14A13A12A1 1A10A9A8
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:
A15A14A13A12A11 A10A9A8A7A6A5A4 A3A2A1A0
it will perform a cirular right shift 5 so that the new int's 16 bits
are:
A12A11A10A9A8A7 A6A5A4A3A2A1A0A 15A14A13
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*********@gma il.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.
>
A15A14A13A12A1 1A10A9A8A7A6A5A 4A3A2A1A0
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*********@gm ail.comwrote in message
news:11******** *************@7 9g2000cws.googl egroups.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:

A15A14A13A12A11 A10A9A8A7A6A5A4 A3A2A1A0

it will output an int in whose 16 bits are:

A7A6A5A4A3A2A1A 0A15A14A13A12A1 1A10A9A8
(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:

A15A14A13A12A11 A10A9A8A7A6A5A4 A3A2A1A0

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

A12A11A10A9A8A7 A6A5A4A3A2A1A0A 15A14A13
(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*********@gma il.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:
>A15A14A13A12A1 1A10A9A8A7A6A5A 4A3A2A1A0

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

What's "it"?
> an int in whose 16 bits are:
>A7A6A5A4A3A2A1 A0A15A14A13A12A 11A10A9A8

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:
>A15A14A13A12A1 1A10A9A8A7A6A5A 4A3A2A1A0
>it will perform a cirular right shift 5 so that the new int's 16 bits
are:
>A12A11A10A9A8A 7A6A5A4A3A2A1A0 A15A14A13

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.de wrote in message
news:4u******** *****@mid.uni-berlin.de...
di*********@gma il.com wrote:
<snip>
>A15A14A13A12A1 1A10A9A8A7A6A5A 4A3A2A1A0

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
4393
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? in java there are 3 kinds of bit shifts: << (shift left) >> (preserve the sign bit as we move right ) >>> (0 filled on the left as we move right)
17
1569
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 l_f_name = Request.Form("txt_Name") l_f_personstitle = Request.Form("txt_Title") l_f_PhoneAreaCode = Request.Form("txt_PhoneAreaCode") l_f_Phone1 = Request.Form("txt_Phone1") l_f_Phone2 = Request.Form("txt_Phone2") l_f_Date =...
9
8728
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 exactly the number of bits in a value... i.e. uint32_t x = 5;
8
3662
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 that it copies one block of data to another block of data until the block that is being copied contains a zero/null. the difference with this code is that it's doing 4bits at a time (all the values are 4bits) and the two blocks of data may not be...
2
2053
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 light please? // Allocate array for( i = 0; i < Length; i++ ) { //pArray_00 is a BYTE Array -- Here a cast is used because
10
10687
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
2501
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 have unsigned char x = 1; is it always true that (x << 1) == 2 (x << 2) == 4 etc?
16
1550
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
1862
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 appears in another record. This incident happened again, this time adding a new wrinkle to the situation. There are two tables -- TableA and TableB -- which have a one-to-one relationship with each other, joined on TableA's autonumber primary key...
12
2251
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 definition or CPU issue? main() { printf("%d\n",(int)0xFFFFFFFF >1); printf("%d\n",(int)-1 >1); }
0
8253
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8189
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8635
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8354
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7182
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5570
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4089
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4192
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2621
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.