473,663 Members | 2,867 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Bit shifting versus architecture question.


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?

Or might I have to reverse the direction of the shift on some
architectures?

(I'm grasping at straws trying to figure out why some C software
written on my i86 system is failing when compiled on another
individual's PPC system.)

Thanks for your help.

Regards,
Charles Sullivan
Feb 7 '07 #1
20 2503
Charles Sullivan <cw******@triad .rr.comwrites:
E.g, if I have
unsigned char x = 1;
is it always true that
(x << 1) == 2
(x << 2) == 4
etc?
Yes, that's always true. The shift operators in C are defined in
terms of numeric values, not how those values are represented.
--
"This is a wonderful answer.
It's off-topic, it's incorrect, and it doesn't answer the question."
--Richard Heathfield
Feb 7 '07 #2
Charles Sullivan wrote On 02/07/07 14:46,:
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?
Yes.
Or might I have to reverse the direction of the shift on some
architectures?
No. "Order" only comes into play when you assemble two
or more smaller pieces into one larger piece, or decompose
the larger piece into smaller fragments. The shift operators,
like most C operators, works with the value that all the
pieces represent, no matter how it is represented. Thus,
even this:

int x = 128;
assert (x << 1 == 256);
assert (x << 2 == 512);

works identically on all systems, both Big- and LittleEndian,
even though the lone one-bit moves (probably) from one byte
of x to another.
(I'm grasping at straws trying to figure out why some C software
written on my i86 system is failing when compiled on another
individual's PPC system.)
I don't see how the paraphrase you've shown could be
responsible. If it's paraphrased from something a little
larger and you can post an actual snip of the larger thing,
maybe somebody will spot something helpful.

--
Er*********@sun .com
Feb 7 '07 #3
Charles Sullivan wrote:
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?
No. The hardware is not even required to have any "bits". There's nothing that
precludes a C implementation on, say, a ternary computer.
E.g, if I have
unsigned char x = 1;
is it always true that
(x << 1) == 2
(x << 2) == 4
etc?
Yes. The notion of "bit" in C refers to an element of the traditional binary
notation, not to the hardware bit. '1' is always '1' in binary , while '2' is
always '10' in binary. This means that a single left shift will always turn 1
into 2.
Or might I have to reverse the direction of the shift on some
architectures?
No.

--
Best regards,
Andrey Tarasevich
Feb 7 '07 #4
Charles Sullivan wrote, On 07/02/07 19:46:
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?

Or might I have to reverse the direction of the shift on some
architectures?
On unsigned types shifting always behaves the same. The result of right
shifting a negative number is implementation defined.
(I'm grasping at straws trying to figure out why some C software
written on my i86 system is failing when compiled on another
individual's PPC system.)
Start by turning up the warnings on your compiler, understanding
everything it complains about, and fixing it (don't cast just to remove
a warning).

If your program is in standard C (modulo errors) then cut it down to a
sensible size that shows a difference and post it here and we can help.
--
Flash Gordon
Feb 7 '07 #5
Charles Sullivan wrote:
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?
No, bit shifting by N bits is defined as a multiplication or division
by 2**N.
E.g, if I have
unsigned char x = 1;
is it always true that
(x << 1) == 2
(x << 2) == 4
etc?
Yes, that is always true.
Or might I have to reverse the direction of the shift on some
architectures?

(I'm grasping at straws trying to figure out why some C software
written on my i86 system is failing when compiled on another
individual's PPC system.)
If you could shorten the code to a minimal example that displays your
problem, you might get the answer here.

Feb 7 '07 #6
On Wed, 07 Feb 2007 14:46:03 -0500, I wrote:
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?
<remainder snipped>

Thanks guys - Ben, Harald, Andrey, Flash, and Eric.
I just wanted to make sure I wasn't doing something obviously
stupid.

The system involves reading and writing to an external device
which may or may not be cranky connected via a USB->Serial
adapter which may or may not be well-supported by its OS's driver.

At this point I'm pretty sure it's not a C language problem.

I appreciate the time you've all taken to answer my question.

Regards,
Charles Sullivan

Feb 7 '07 #7
On Feb 7, 12:46 pm, Charles Sullivan <cwsul...@triad .rr.comwrote:
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?

Or might I have to reverse the direction of the shift on some
architectures?

(I'm grasping at straws trying to figure out why some C software
written on my i86 system is failing when compiled on another
individual's PPC system.)

Thanks for your help.

Regards,
Charles Sullivan


Are you dealing with any data coming from files or from a network
connection? This is one of the easiest places to get in trouble while
making non-portable assumptions. (The particulars of doing network
stuff are obviously off topic here, but I think it's still on topic to
point out that reading raw data from foreign sources can get you in
trouble.)

Also, bits aren't the only concern. Bytes can be arranged "funny" in
larger types on architectures that aren't your favorite. Lucky for
you, your x86 implimentation and your PPC implimentation probably both
use 8 bit bytes and chars. :)

If the trouble isn't from corrupt external data, then I'd expect that
you may have made a non-portable assumption about types being the
same. A pointer to an int can't portably be treated as a pointer to a
short or a long, etc. If you don't specify if you want a signed or
unsigned char, the implimentation isn't obliged to give you one or the
other.

Off the top of my head, those are the things that I tend to screw up
when I've moved things from x86 to PPC. (All the while, muttering
that I had done everything perfectly, and it must be a compiler
bug... :) )

Feb 7 '07 #8
Charles Sullivan wrote:
(I'm grasping at straws trying to figure out why some C software
written on my i86 system is failing when compiled on another
individual's PPC system.)

Thanks for your help.

Regards,
Charles Sullivan
Sounds like an endian issue (byte-order) rather than a bit-order issue.
Feb 8 '07 #9
On Wed, 07 Feb 2007 17:11:09 -0800, Christopher Layne wrote:
Charles Sullivan wrote:
>(I'm grasping at straws trying to figure out why some C software
written on my i86 system is failing when compiled on another
individual's PPC system.)

Thanks for your help.

Regards,
Charles Sullivan

Sounds like an endian issue (byte-order) rather than a bit-order issue.
Thanks for your comment and taking the time to respond.
At the stage where the problem exists the data is being
handled strictly on a byte-by-byte basis, so byte order
shouldn't enter into it.

Regards,
Charles Sullivan

Feb 8 '07 #10

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

Similar topics

6
4398
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)
31
4778
by: surfunbear | last post by:
I've read some posts on Perl versus Python and studied a bit of my Python book. I'm a software engineer, familiar with C++ objected oriented development, but have been using Perl because it is great for pattern matching, text processing, and automated testing. Our company is really fixated on risk managnemt and the only way I can do enough testing without working overtime (which some people have ended up doing) is by automating my...
4
2296
by: apngss | last post by:
what's the differences between collocated architecture and distributed architecture?? My understanding is that collocated architecture has everything in a single machine? i.e. There is only 1 machine in middle business tier? But distributed architecture distributes the load in multiple machines? i.e. There are multiple machines in middle business tier?
4
1662
by: ben | last post by:
hiyer, say you've got a 32 bit int - is there a nifty way to shift that whole value right by 4 bits and at the same time collect the 4 bits that are being shifted off the edge to the right into another variable? i'm hoping for a way to do it in one foul swoop ... ? probably not, but just wondering if there's a better way than the obvious : x = 0xf & bits; bits >>= 4;
8
1647
by: Nick Patavalis | last post by:
Ok, what is this supposed to print: #include <stdio.h> #include <stdint.h> int main (void) { uint32_t r, s;
2
2055
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
6
1323
by: dcbud | last post by:
I'm hoping to get a response from developers with experience in both developing applications for Windows and the Web using VS.NET2005. I'm looking for input as to why we would want to develop a web application versus a Windows Application. I know all the standard reasons, Availability, Easier Deployment (although that is really not an issue any more with Windows Apps in .NET), etc, I'm looking for more better reasons why we should develop...
18
4713
by: richard_l | last post by:
Hello All, I am writing an application which receives a word which is a bitmap. I have created a word typedef which contains a bitfield defining each bit. however, I was wondering however if it would be better to write a macro to access each bit instead of the bitfield. I have read the C-FAQ on bit fields, but was wondering if there were any advantages/disadvantages to using bit shifting. To my mind I think using bitfields are more...
4
1864
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...
0
8436
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
8345
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
8771
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...
0
8634
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6186
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5657
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();...
1
2763
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
2
2000
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1757
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.