473,804 Members | 2,271 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

1>>x behaviour

Hello,

I have a little question about operator >between integers: does
anyone know why, when I write:
"1 >x", the returned value is 1 for all values multiple of 32 and 0
else (this does not hurt me, 1 does).
When I do the same thing with : "1L >x", only multiples of 64 do
this.
I thought 0 was the only possible result for any x 0.
Why is it not the case ? And how to correct this simply -- in one
instruction, else I did it by:

long longFoo = intBar;
longFoo >>= x;
return (int) longFoo;

.... but this looks weird to do this.

Thanks in advance to any answer.

Nov 23 '06 #1
10 1359
Hi Vivien,

The shift operation is done using the lowest 5 bits of the x operand for
Int32, and lowest 6 bits for Int64.

1 >32 is the same as 1 >0
On Thu, 23 Nov 2006 12:20:55 +0100, Vivien Parlat <po****@gmail.c omwrote:
Hello,

I have a little question about operator >between integers: does
anyone know why, when I write:
"1 >x", the returned value is 1 for all values multiple of 32 and 0
else (this does not hurt me, 1 does).
When I do the same thing with : "1L >x", only multiples of 64 do
this.
I thought 0 was the only possible result for any x 0.
Why is it not the case ? And how to correct this simply -- in one
instruction, else I did it by:

long longFoo = intBar;
longFoo >>= x;
return (int) longFoo;

... but this looks weird to do this.

Thanks in advance to any answer.


--
Happy Coding!
Morten Wennevik [C# MVP]
Nov 23 '06 #2
Jay
So is that a bug?

"Morten Wennevik" <Mo************ @hotmail.comwro te in message
news:op******** *******@tr024.b ouvet.no...
Hi Vivien,

The shift operation is done using the lowest 5 bits of the x operand for
Int32, and lowest 6 bits for Int64.

1 >32 is the same as 1 >0
On Thu, 23 Nov 2006 12:20:55 +0100, Vivien Parlat <po****@gmail.c omwrote:
Hello,

I have a little question about operator >between integers: does
anyone know why, when I write:
"1 >x", the returned value is 1 for all values multiple of 32 and 0
else (this does not hurt me, 1 does).
When I do the same thing with : "1L >x", only multiples of 64 do
this.
I thought 0 was the only possible result for any x 0.
Why is it not the case ? And how to correct this simply -- in one
instruction, else I did it by:

long longFoo = intBar;
longFoo >>= x;
return (int) longFoo;

... but this looks weird to do this.

Thanks in advance to any answer.


--
Happy Coding!
Morten Wennevik [C# MVP]
Nov 24 '06 #3
On Fri, 24 Nov 2006 10:14:50 +0100, Jay <nospamwrote:
So is that a bug?
It's by design. When you think about it, shifting a 32 bit integer with
anything more than 31 places doesn't make any sense.

http://msdn2.microsoft.com/en-us/library/xt18et0d.aspx
--
Happy Coding!
Morten Wennevik [C# MVP]
Nov 24 '06 #4
What does shift do? I read this thread and was intrigued but don't
understand it's use.
I am just learning the basics.

Please advise.

Morten Wennevik wrote:
On Fri, 24 Nov 2006 10:14:50 +0100, Jay <nospamwrote:
So is that a bug?

It's by design. When you think about it, shifting a 32 bit integer with
anything more than 31 places doesn't make any sense.

http://msdn2.microsoft.com/en-us/library/xt18et0d.aspx
--
Happy Coding!
Morten Wennevik [C# MVP]
Nov 24 '06 #5
But there is an oddity here;

shifting by 5 and then by 7 is equivalent to shifting by 12
shifting by 16 and then by 16 would leave nothing; yet shifting by 32 in one
go doesn't always...

Intriguing, but not a huge problem generally...

Marc
Nov 24 '06 #6
In brief, it works on the bitwise representation of a value, and moves
everything left or right by {x} places, dropping bits off one end, and
back-filling with 0s at the other.

e.g. integer 1 in binary is ...00001 (=int 1); left shift gives ...00010
(=int 2); right shift this twice yields ...00000 (=int 0)

http://msdn.microsoft.com/library/de...ftoperator.asp

Marc
Nov 24 '06 #7
Hi,

Shifting is used when you need information at bitlevel

Consider the number 68437143. In the file it would be represented as

00000100 00010100 01000100 10010111

Hidden inside is the dimensions of an image where the information is
arranged as

0000 010000 01010001000 10010010111
Unused Colors Width Height
4 6 11 11

To be able to get this information we need to do bit manipulation
By filtering the number with 2047 we get the lower 11 bits

000001000001010 001000100100101 11 (68437143)
&
000000000000000 000000111111111 11 (2047)
=
000000000000000 000000100100101 11 (1175)
[code]
int n = 68437143;
int filter = 2047
int Height = n & filter; // 1175
To get Width we shift the bits 11 places to the right

000001000001010 001000100100101 11 (68437143)
>11
000000000000000 010000010100010 00 (33416) 10010010111 has been pushed out

[code]
n = n >11;

Do the same filtering to get the Width

000000000000000 010000010100010 00 (33416)
&
000000000000000 000000111111111 11 (2047)
=
000000000000000 000000010100010 00 (648)

Shift right 11 more places

000000000000000 010000010100010 00 (33416)
>11
000000000000000 000000000000100 00 (16)

Summarized in code it will look like

int number = 68437143;
int filter = 2047;
int height = number & filter;
number = number >11; // you could also write number >>= 11
int width = number & filter;
int color = number >11;

Image size = 648x1175x16

For instance, image files pack values int bits, not bytes.

On Fri, 24 Nov 2006 10:44:45 +0100, <ga********@myw ay.comwrote:
What does shift do? I read this thread and was intrigued but don't
understand it's use.
I am just learning the basics.

Please advise.

Morten Wennevik wrote:
>On Fri, 24 Nov 2006 10:14:50 +0100, Jay <nospamwrote:
So is that a bug?

It's by design. When you think about it, shifting a 32 bit integer with
anything more than 31 places doesn't make any sense.

http://msdn2.microsoft .com/en-us/library/xt18et0d.aspx
--
Happy Coding!
Morten Wennevik [C# MVP]


--
Happy Coding!
Morten Wennevik [C# MVP]
Nov 24 '06 #8
Jay
Yes it doesn't make sense, but it ought to do something sensible rather than something quirky. For
example, if you shift by more than 32 bits, it should set the variable to zero (assuming unsigned
variables for now) since this is the true answer if you did shift more than 32 bits.

"Morten Wennevik" <Mo************ @hotmail.comwro te in message
news:op******** *******@tr024.b ouvet.no...
On Fri, 24 Nov 2006 10:14:50 +0100, Jay <nospamwrote:
So is that a bug?
It's by design. When you think about it, shifting a 32 bit integer with
anything more than 31 places doesn't make any sense.

http://msdn2.microsoft.com/en-us/library/xt18et0d.aspx
--
Happy Coding!
Morten Wennevik [C# MVP]
Nov 24 '06 #9
On Fri, 24 Nov 2006 14:22:45 +0100, Jay <nospamwrote:
Yes it doesn't make sense, but it ought to do something sensible rather
than something quirky. For
example, if you shift by more than 32 bits, it should set the variable
to zero (assuming unsigned
variables for now) since this is the true answer if you did shift more
than 32 bits.
Not if there is more information in the shift parameter than should be
used for shifting. For instance, if you have a value that needs to be
shiftet several times each time a different number of places. You could
then store all the shift values in a single integer and shift the shift
value n places each shift.

int someNumber = 12345667;
int shiftNumber = 12345;

int value1 = someNumber;
someNumber >>= shiftNumber;
shiftNumber >>= 5;
int value2 = someNumber;
someNumber >>= shiftNumber
shiftNumber >>= 5
....

I can't see much reason to use it, but there may be some performance to be
gained in a heavy duty loop.
--
Happy Coding!
Morten Wennevik [C# MVP]
Nov 24 '06 #10

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

Similar topics

11
2715
by: brendan | last post by:
Sorry this isnt a cross post .. i just didnt get any help from alt.php. I have a website which utilises post forms for navigation in some areas. Problem is, when *some* users hit the BACK button the POSTDATA content has expired and they need to refresh the page then they get a alert about refreshing expired data. I am getting complaints that this is too annoying and limits the sites useability.
13
2508
by: dpj5754 | last post by:
Is there a simple and determinist way to make the difference between the 2 sequences: <XX></XX> and <XX/> The EndElement callback does not provide this information.
11
13732
by: Les Paul | last post by:
I'm trying to design an HTML page that can edit itself. In essence, it's just like a Wiki page, but my own very simple version. It's a page full of plain old HTML content, and then at the bottom, there's an "Edit" link. So the page itself looks something like this: <HTML><HEAD><TITLE>blah</TITLE></HEAD><BODY> <!-- TEXT STARTS HERE --> <H1>Hello World!</H1> <P>More stuff here...</P>
38
24012
by: Xah Lee | last post by:
sometimes i wish to add white space in <p> as to achived effects similar to tab. what should i do? using empty image seems the sure way but rather complicated. (and dosen't change size with font) Woudl some of the space character in unicode work? (my html files uses unicode) Xah
40
2524
by: fordge | last post by:
we usually use <<n as a cheaper option to multiplication when its factor of 2^n right, even for non 2^n say ix24 ==> ix(16+8) ==> (i<<4)+(i<<3) similarly divison 2^n is >>n
15
2493
by: kernel.lover | last post by:
Hello, i want to know to have multiple clients connects to same server program does following is correct code #include <sys/types.h> #include <sys/socket.h> #include <stdio.h> #include <netinet/in.h> #include <signal.h> #include <unistd.h>
16
1884
by: soorajk007 | last post by:
Hi friends, Im not an expert in the low level activities of C and I need to know how the following works- what is the difference b/w the way -1<<4 and -1>>4 works?? Plz plz explain bit by bit as much as possible... Thanx in advance.. Bye Sooraj
28
5382
by: Kent Feiler | last post by:
1. Here's some html from a W3C recommendations page. <P>aaaaaaaaa<DIV>bbbbbbbbb</DIV><DIV>cccccccc<P>dddddddd</DIV> 2.Although I didn't think it would make any difference, I tried it with the </p>s included as well. <P>aaaaaaaaa</p><DIV>bbbbbbbbb</DIV><DIV>cccccccc<P>dddddddd</p></DIV>
0
278
by: Weeble | last post by:
On Jun 20, 11:10 pm, Göran Andersson <gu...@guffa.comwrote: I did it that way because CopyTo has a whole bunch of failure cases to take care of (multi-dimensional array, not enough space, negative index, null array), and I didn't think it was likely to get used that much, so I wanted to do something that would behave correctly without a lot of work. If it became a performance issue I would certainly have changed it later.
0
9714
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
9594
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
10346
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
10347
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
9173
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
5531
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
5673
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4308
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
3832
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.