473,394 Members | 1,828 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,394 software developers and data experts.

binary algorithm

281 100+
I do really need to consult you guys about java codes.
Please give me some pointer where should I refer regarding 'binary concept' especially when it does involve with such this '>>'.
What does it mean or to say in word?

Below I paste codes for binary gdc computation....please advise...many thanks.

Expand|Select|Wrap|Line Numbers
  1. private static long binaryAlgorithm (long u, long v) 
  2.  {
  3.                  long gcd = 0;
  4.         long k = 0;
  5.         long t = 0;
  6.  
  7.         u = Math.abs (u);
  8.         v = Math.abs (v);
  9.         // Find power of 2
  10.         while (true) 
  11.         {
  12.           if (isOdd (u)) 
  13.           {
  14.             break;
  15.           }
  16.  
  17.           if (isOdd (v)) 
  18.           {
  19.             break;
  20.           }
  21.           k++;
  22.           u = u >> 1;
  23.           v = v >> 1;          
  24.         }
Apr 1 '07 #1
3 1774
JosAH
11,448 Expert 8TB
I ripped this straight from the JLS Java Language Specification. Here goes:

The shift operators include left shift <<, signed right shift >>, and unsigned right
shift >>>; they are syntactically left-associative (they group left-to-right). The
left-hand operand of a shift operator is the value to be shifted; the right-hand
operand specifies the shift distance.

The type of each of the operands of a shift operator must be a type that is
convertible to a primitive integral type, or a compile-time error occurs. Binary
numeric promotion is not performed on the operands; rather, unary numeric
promotion is performed on each operand separately. The type of the shift
expression is the promoted type of the left-hand operand.

If the promoted type of the left-hand operand is int, only the five lowest-order
bits of the right-hand operand are used as the shift distance. It is as if the right-
hand operand were subjected to a bitwise logical AND operator & with the mask
value 0x1f. The shift distance actually used is therefore always in the range 0 to
31, inclusive.

If the promoted type of the left-hand operand is long, then only the six lowest-
order bits of the right-hand operand are used as the shift distance. It is as if the
right-hand operand were subjected to a bitwise logical AND operator & with the
mask value 0x3f. The shift distance actually used is therefore always in the
range 0 to 63, inclusive.

At run time, shift operations are performed on the two's complement integer
representation of the value of the left operand.

The value of n<<s is n left-shifted s bit positions; this is equivalent (even if
overflow occurs) to multiplication by two to the power s.

The value of n>>s is n right-shifted s bit positions with sign-extension. The
resulting value is n/s. For nonnegative values of n, this is equivalent to
truncating integer division, as computed by the integer division operator /, by
two to the power s.

The value of n>>>s is n right-shifted s bit positions with zero-extension. If n is
positive, then the result is the same as that of n>>s; if n is negative, the result
is equal to that of the expression (n>>s)+(2<<~s) if the type of the left-hand
operand is int, and to the result of the expression (n>>s)+(2L<<~s) if the type
of the left-hand operand is long. The added term (2<<~s) or (2L<<~s) cancels
out the propagated sign bit. (Note that, because of the implicit masking of the
right-hand operand of a shift operator, ~s as a shift distance is equivalent to
31-s when shifting an int value and to 63-s when shifting a long value.)


kind regards,

Jos
Apr 1 '07 #2
shana07
281 100+
I ripped this straight from the JLS Java Language Specification. Here goes:

The shift operators include left shift <<, signed right shift >>, and unsigned right
shift >>>; they are syntactically left-associative (they group left-to-right). The
left-hand operand of a shift operator is the value to be shifted; the right-hand
operand specifies the shift distance.

The type of each of the operands of a shift operator must be a type that is
convertible to a primitive integral type, or a compile-time error occurs. Binary
numeric promotion is not performed on the operands; rather, unary numeric
promotion is performed on each operand separately. The type of the shift
expression is the promoted type of the left-hand operand.

If the promoted type of the left-hand operand is int, only the five lowest-order
bits of the right-hand operand are used as the shift distance. It is as if the right-
hand operand were subjected to a bitwise logical AND operator & with the mask
value 0x1f. The shift distance actually used is therefore always in the range 0 to
31, inclusive.

If the promoted type of the left-hand operand is long, then only the six lowest-
order bits of the right-hand operand are used as the shift distance. It is as if the
right-hand operand were subjected to a bitwise logical AND operator & with the
mask value 0x3f. The shift distance actually used is therefore always in the
range 0 to 63, inclusive.

At run time, shift operations are performed on the two's complement integer
representation of the value of the left operand.

The value of n<<s is n left-shifted s bit positions; this is equivalent (even if
overflow occurs) to multiplication by two to the power s.

The value of n>>s is n right-shifted s bit positions with sign-extension. The
resulting value is n/s. For nonnegative values of n, this is equivalent to
truncating integer division, as computed by the integer division operator /, by
two to the power s.

The value of n>>>s is n right-shifted s bit positions with zero-extension. If n is
positive, then the result is the same as that of n>>s; if n is negative, the result
is equal to that of the expression (n>>s)+(2<<~s) if the type of the left-hand
operand is int, and to the result of the expression (n>>s)+(2L<<~s) if the type
of the left-hand operand is long. The added term (2<<~s) or (2L<<~s) cancels
out the propagated sign bit. (Note that, because of the implicit masking of the
right-hand operand of a shift operator, ~s as a shift distance is equivalent to
31-s when shifting an int value and to 63-s when shifting a long value.)


kind regards,

Jos
Thanks Jos...I read it..and I just still don't get it, why in order to perform gcd calculation, it involves with shift operation (binary)? i don't see the rational behind it. please share few words about it. Thanks.
Apr 1 '07 #3
JosAH
11,448 Expert 8TB
Thanks Jos...I read it..and I just still don't get it, why in order to perform gcd calculation, it involves with shift operation (binary)? i don't see the rational behind it. please share few words about it. Thanks.
Well, your algorithm isn't a binary gcd algorithm (it just has some slight
resemblances with it). For a full explanation read this link.

kind regards,

Jos
Apr 1 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

16
by: Mars | last post by:
I'm writing a program for listing all binary numbers of the same length with the same number of 1s. e.g. 0011 0101 0110 1001 1010 1100
6
by: Ravi | last post by:
Hi, I need to find the non-recursive algorithm to find the height of a Binary Tree. Regards, SunLight.
15
by: Foodbank | last post by:
Hi all, I'm trying to do a binary search and collect some stats from a text file in order to compare the processing times of this program (binary searching) versus an old program using linked...
9
by: Ching-Lung | last post by:
Hi all, I try to create a tool to check the delta (diff) of 2 binaries and create the delta binary. I use binary formatter (serialization) to create the delta binary. It works fine but the...
3
by: Peter Schmitz | last post by:
Hi, one of the main task of my currently developed application is to search in small buffers for given patterns (average buffer size 1000 bytes). Now, for some reasons I can't use the provided C...
10
by: free2cric | last post by:
Hi, I have a single link list which is sorted. structure of which is like typedef struct mylist { int num; struct mylist *next;
4
by: Hunk | last post by:
Hi I have a binary file which contains records sorted by Identifiers which are strings. The Identifiers are stored in ascending order. I would have to write a routine to give the record given...
6
by: Alexander Vasilevsky | last post by:
How do I know whether the file is text or binary? http://www.alvas.net - Audio tools for C# and VB.Net developers + Christmas discount
6
Kelicula
by: Kelicula | last post by:
Why?: One commonly used algorithm is the binary search. If you don't already know it, you should read on. Very helpful. Saves much CPU. Reduces computations exponentially. When searching...
6
by: aznimah | last post by:
hi, i'm work on image comparison. i'm using the similarity measurement which i need to: 1) convert the image into the binary form since the algorithm that i've use works with binary data for the...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
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
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...

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.