humansg
asked on
Question about bytes and FileOutputStream
File outfile = new File( "Test.bin" );
FileOutputStream out = new FileOutputStream( outfile );
byte data = 0x30; // "0" in hex
out.write( data^3 );
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) );
How the question has changed?
ASKER
I added another question on this
What bits are manipulated in ^3?
out.write( data[i] | (1 << 1) );
What bits are manipulated in ^3?
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
and this
data | 2
makes bitwise or-ing ads second bit to whatvere bits there are in data
what is
data[i]
- data is not an array
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Sorry, my mistake.
It should be
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.
It should be
out.write( data | (1 << 1) );
Assuming data to be 0x30byte data = 0x30;
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.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Actually 50 is ASCII for "2" then it makes sense
ASKER
Great! Thanks for the enlightenment!
data^3 is not the power of 3 - it is bit operation