473,624 Members | 2,160 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Bit Shifting problem with endieness

I am having the following problem of bit shifting.

My program runs on a little endian machine. Consider that if I have
the following data represented in big endian...

0x12345678

the little endian representation would be

0x78563412.

Now if I wan to shift my original data by 4 bits to 0x01234567, then
my approach would be

val = 0x12345678
htonl(htonl(val )>>4)

and result represented in my machine is

0x67452301.

This looks quite redundant to convert little endian to Bid Endian
shift it and then convert back. Basically I thinking of how to get rid
of those two htonl system calls.

I would like to know is there any better way to do this. A best
possibility to shift in little endian mode??
Mar 10 '08 #1
6 2475
Madhur wrote:
I am having the following problem of bit shifting.

My program runs on a little endian machine. Consider that if I have
the following data represented in big endian...

0x12345678

the little endian representation would be

0x78563412.

Now if I wan to shift my original data by 4 bits to 0x01234567, then
my approach would be

val = 0x12345678
htonl(htonl(val )>>4)

and result represented in my machine is

0x67452301.

This looks quite redundant to convert little endian to Bid Endian
shift it and then convert back. Basically I thinking of how to get rid
of those two htonl system calls.

I would like to know is there any better way to do this. A best
possibility to shift in little endian mode??
I don't think you have an endianess problem:
$ cat a.c
#include <stdio.h>
int main(void) {
int a=0x12345678;
printf("0x%x >4 = 0x%x\n", a, a >4);
return 0
}
$ cc a.c
$ ./a.out
0x122345678 >4 = 0x1234567

Same output on big and little endian machine...

Bye, Jojo
Mar 10 '08 #2
Madhur <ma********@gma il.comwrote:
I am having the following problem of bit shifting.

My program runs on a little endian machine.
Bit shifting is defined to work on values, not on their memory
representations , so the endianness of your machine does not matter.

Richard
Mar 10 '08 #3
On Mar 10, 5:35 pm, "Joachim Schmitz" <nospam.j...@sc hmitz-digital.de>
wrote:
Madhur wrote:
I am having the following problem of bit shifting.
My program runs on a little endian machine. Consider that if I have
the following data represented in big endian...
0x12345678
the little endian representation would be
0x78563412.
Now if I wan to shift my original data by 4 bits to 0x01234567, then
my approach would be
val = 0x12345678
htonl(htonl(val )>>4)
and result represented in my machine is
0x67452301.
This looks quite redundant to convert little endian to Bid Endian
shift it and then convert back. Basically I thinking of how to get rid
of those two htonl system calls.
I would like to know is there any better way to do this. A best
possibility to shift in little endian mode??

I don't think you have an endianess problem:
$ cat a.c
#include <stdio.h>
int main(void) {
int a=0x12345678;
printf("0x%x >4 = 0x%x\n", a, a >4);
return 0}

$ cc a.c
$ ./a.out
0x122345678 >4 = 0x1234567

Same output on big and little endian machine...

Bye, Jojo
On a big-endian processor, 0x12345678 is stored in consecutive bytes
as 0x12 0x34 0x56 0x78,
while on a little-endian processor it is stored as 0x78 0x56 0x34
0x12.
I am trying to manupulate a buffer neither of ints and longs. As
mentioned if I have a large buffer say 1K bytes to shift by 4 bits,
the problem definitely persists.
Mar 10 '08 #4
Madhur wrote:
On a big-endian processor, 0x12345678 is stored in consecutive bytes
as 0x12 0x34 0x56 0x78,
while on a little-endian processor it is stored as 0x78 0x56 0x34
0x12.
I am trying to manupulate a buffer neither of ints and longs. As
mentioned if I have a large buffer say 1K bytes to shift by 4 bits,
the problem definitely persists.
You say "As mentioned" but I don't see any reference to this previously.

Why do you want to shift 1K bytes by 4 bits? Would it be easier and/or
faster to use a circular buffer with mutable start/end indices instead?

Phil
Mar 10 '08 #5
Madhur wrote:
>
On Mar 10, 5:35 pm, "Joachim Schmitz" <nospam.j...@sc hmitz-digital.de>
wrote:
Madhur wrote:
[... bit-shifting using htonl/shift/ntohl to "solve" the little-endian ...]
[... "problem". ...]
I am having the following problem of bit shifting.
[...]
I don't think you have an endianess problem:
$ cat a.c
#include <stdio.h>
int main(void) {
int a=0x12345678;
printf("0x%x >4 = 0x%x\n", a, a >4);
return 0}

$ cc a.c
$ ./a.out
0x122345678 >4 = 0x1234567

Same output on big and little endian machine...

Bye, Jojo

On a big-endian processor, 0x12345678 is stored in consecutive bytes
as 0x12 0x34 0x56 0x78,
while on a little-endian processor it is stored as 0x78 0x56 0x34
0x12.
True, but irrelevent.
I am trying to manupulate a buffer neither of ints and longs. As
mentioned if I have a large buffer say 1K bytes to shift by 4 bits,
the problem definitely persists.
You're overcomplicatin g things.

You aren't "manipulati ng a buffer", you're "manipulati ng a _value_".
when you have:

int a = 0x12345678;

it doesn't matter whether you are on a big- or little-endian system.
When you execute:

int b = a >4;

you are not treating a as a buffer of (4, in this case) unrelated
bytes. Rather, you are treating it as a single (32-bit, in this
case) value.

At the machine code, you may end up with something like this:

move eax,_a ; This stores 0x12345678 into the eax register
sar eax,4 ; ">4"
move _b,eax ; This stores 0x01234567 into b

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer .h|
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:Th***** ********@gmail. com>
Mar 10 '08 #6

"Madhur" <ma********@gma il.comwrote in message
news:f8******** *************** ***********@s19 g2000prg.google groups.com...
On Mar 10, 5:35 pm, "Joachim Schmitz" <nospam.j...@sc hmitz-digital.de>
wrote:
>Madhur wrote:
I am having the following problem of bit shifting.
On a big-endian processor, 0x12345678 is stored in consecutive bytes
as 0x12 0x34 0x56 0x78,
while on a little-endian processor it is stored as 0x78 0x56 0x34
0x12.
I am trying to manupulate a buffer neither of ints and longs. As
mentioned if I have a large buffer say 1K bytes to shift by 4 bits,
the problem definitely persists.
The problem needs to be specified more precisely. If the first few bytes of
your buffer are:

0x12 0x34 0x56 0x78 ...

then each byte is being displayed high-nibble (or nybble) first and a 4-bit
shift of the entire buffer is ambiguous. If you display your data as:

0x1 0x2 0x3 0x4 0x5 0x6 0x7 0x8 ...

Then a left-shift of 4-bits (1 nibble) is unambiguous (you end up with 0x2,
0x3 etc). But you need to define how pairs of nibbles are represented in
each byte (as 12, 34 or 21, 43).

It might well be simpler to just store one nibble in each byte; will waste
memory but shifting is far simpler.

Or, if you want to shift 32 bits at a time, reorganise each group of 8
nibbles according to your memory layout, perhaps as 0x87654321 (a left shift
becomes a 32-bit right shift) or as 0x12345678 (left shift is still right
shift). But the memory layout in the latter case might be 0x78 0x56 0x34
0x12.

--
Bart
Mar 10 '08 #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;
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
8173
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
8679
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8621
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
8475
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
6110
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
5563
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
4079
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...
1
2606
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
1482
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.