Solved

Question about bytes and FileOutputStream

Posted on 2011-09-07
10
213 Views
Last Modified: 2012-08-13
File outfile = new File( "Test.bin" );
FileOutputStream out = new FileOutputStream( outfile );

byte data = 0x30; // "0" in hex

out.write( data^3 );

Open in new window


The output in the file when opened with WordPad is "3"

^3 is power to 3. Can anyone explain how it became 3?


And what is this doing?
out.write( data | (1 << 1) );

Open in new window

0
Comment
Question by:humansg
  • 7
  • 3
10 Comments
 
LVL 47

Expert Comment

by:for_yan
ID: 36497206

data^3 is not the power of 3 - it is bit operation
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36497212
How the question has changed?
0
 

Author Comment

by:humansg
ID: 36497226
I added another question on this
out.write( data[i] | (1 << 1) );

Open in new window


What bits are manipulated in ^3?
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 47

Expert Comment

by:for_yan
ID: 36497229
1<<1 shifts bit one position to the left - it becomes 2 as i understand

and this

data | 2

makes bitwise or-ing ads second bit to whatvere bits there are in data
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36497243
what is
data[i]

Open in new window

- data is not an array
0
 
LVL 47

Accepted Solution

by:
for_yan earned 500 total points
ID: 36497283
So initially adata is 48 when you or with 2 it becomes 50 - all as exepcted:

read here:
http://leepoint.net/notes-java/data/expressions/bitops.html

see the result of my compuilation and run of it:

    int data = 0x30;

        System.out.println("datat before " + data);
        System.out.println("data after: " + (data | (1<<1)));

Open in new window



output:

datat before 48
data after: 50

Open in new window

0
 

Author Comment

by:humansg
ID: 36497307
Sorry, my mistake.
It should be
out.write( data | (1 << 1) );

Open in new window

Assuming data to be 0x30
byte data = 0x30;

Open in new window


0x30 in binary is 0011 0000
Does that mean it becomes 0110 0000 after applying this (1<<1)?
And after which we will perform a bitwise OR operation
0011 0000 | 0110 0000 = 0111 0000?  (ascii "F")
This don't seems right because the output for this operation is "2" when open with WordPad.
0
 
LVL 47

Assisted Solution

by:for_yan
for_yan earned 500 total points
ID: 36497345
No you first apply this (1<<1) - it becomes 2
and then your OR 00110000 with 10 and it becomes 50 as I shouwed above
Put System.out.println() before you write it to the file - it should be 50
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36497352
Actually 50 is ASCII for "2" then it makes sense
0
 

Author Closing Comment

by:humansg
ID: 36497385
Great! Thanks for the enlightenment!
0

Featured Post

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
groupSumClump challenge 9 115
HashTable highest marks enumeration alternative 9 39
How to convert String matching to regex in java 4 34
mysql jsp example issue 32 38
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question