Previous Thread
Next Thread
Print Thread
Rate Thread
Java question, Pergesu where are you!!! #19066
01/03/04 08:22 AM
01/03/04 08:22 AM
Joined: Dec 2002
Posts: 3,255
Likes: 3
Maryland
§intå× Offline OP
UGN Elite
§intå×  Offline OP
UGN Elite

Joined: Dec 2002
Posts: 3,255
Maryland
I am studying java, actually I am taking a CBT course through work. I am studying shift opperators and do not understand quite how I would use this in code. Now this sounds most handy for something like encryption, but how to I get access to the acctual stream of 1's and 0's in the data stream?

<< and >> and >>>

Please give an example of when I would use this.

Re: Java question, Pergesu where are you!!! #19067
01/03/04 08:23 AM
01/03/04 08:23 AM
Joined: Dec 2002
Posts: 3,255
Likes: 3
Maryland
§intå× Offline OP
UGN Elite
§intå×  Offline OP
UGN Elite

Joined: Dec 2002
Posts: 3,255
Maryland
Also The course is saying the << is use for when doing multiplication by 2 and >> is useful when doing division by 2. why not just int*2 and int/2

Re: Java question, Pergesu where are you!!! #19068
01/03/04 10:25 PM
01/03/04 10:25 PM
Joined: Jan 2004
Posts: 6
TN, USA
V
vooduhal Offline
Junior Member
vooduhal  Offline
Junior Member
V

Joined: Jan 2004
Posts: 6
TN, USA
Let me give you an example of how this could be useful. I wrote this a while back for a goofy little programming language. The language is c but it still applies.
Code
#define CHAR_LENGTH 8


// char *CharToBinary(char a_chChar)
// Accepts:  A single character
// Returns:  The binary string equivalent of the the input character
// Purpose:  Converts the given char to its binary string format
char *char2binary(char a_chChar) {
	char 	 *p_chBuffer = (char *)malloc(CHAR_LENGTH); 	// This will hold our converted buffer
	int	 intCtr=0; 		// You can figure this one out
	
	// Now let's just loop through each bit
	for(intCtr=0; intCtr<CHAR_LENGTH; intCtr++) 
		// We are just going to use a binary & to determine the value of each bit
		p_chBuffer[7 - intCtr] = (((a_chChar & (1 << intCtr)) == ( 1 << intCtr)) ? 0x31 : 0x30);
	
	// Ok we're done
	return p_chBuffer;
}	
Hope that helps. Basically, realizes that each bit can be seperated by using powers of 2, ie. 00000010 = 2, 00000100 = 4 etc. I use that fact to create a quick little loop to break the byte down. Let me know if you need any explanation.

Re: Java question, Pergesu where are you!!! #19069
01/04/04 02:38 PM
01/04/04 02:38 PM
Joined: Mar 2002
Posts: 1,136
Pimpin the Colorizzle
P
pergesu Offline
UGN Elite Poster
pergesu  Offline
UGN Elite Poster
P

Joined: Mar 2002
Posts: 1,136
Pimpin the Colorizzle
Alright, here's a quick example.

Code
public class test {
        public static void main(String args[]) {
                int i = 5 << 4;
                System.out.println(i);
        }
}
The << operator in this case is used to multiply 5 by 2^4, which is 16. Thus the output is 80.

5 * [2^4]

Likewise, the >> operator would be used to multiply the left value by 2^rvalue.

You very rarely use either of these. They're useful if you need to multiply or divide by a power of 2. Otherwise, you don't use them.

To be honest, I've never used the >>> operator, and don't know what it does off the top of my head.

Using bitshift operators on data streams is pointless, as well as bad practice. You should use the methods provided by the stream classes to access data.

In five years of doing Java coding, I don't believe I've ever used any of these operators. So I really don't know too much about them, except that basic example I gave above. Just goes to show how useful they really are *wink* since for some reason I can't use a parentheses


Member Spotlight
Crime
Crime
SC, usa
Posts: 508
Joined: March 2002
Show All Member Profiles 
Forum Statistics
Forums41
Topics33,680
Posts68,696
Average Daily Posts0
Members2,175
Most Online1,567
Apr 25th, 2010
Latest Postings
Blackbeard.....
by Gremelin. 10/04/18 02:02 AM
my old account still exists!
by Crime. 08/10/18 09:47 PM
Top Posters(All Time)
UGN Security 41,392
Gremelin 7,203
§intå× 3,255
SilentRage 1,273
Ice 1,146
pergesu 1,136
Infinite 1,041
jonconley 955
Girlie 908
unreal 860
Top Liked Users (All Time)
§intå× Likes: 3
Black Beard Likes: 1
Cold Sunn Likes: 1
Crime Likes: 1
Cyrez Likes: 1
Dartur Likes: 1
fleshwound Likes: 1
Ghost Likes: 2
Girlie Likes: 1
Gremelin Likes: 14
Powered by UBB.threads™ PHP Forum Software 7.6.3
(Snapshot build 20181002)