关于byte的与运算 byte b = by[x];//by是一个字节数组,x的初始值是int型的0,也就是取字节数组by的第一个字节return b&0xff;byte占一个8位,与255之后会不会改变byte的原值呢?为什么? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 import java.io.*;class MyBufferedInputStream { InputStream is; byte[] by = new byte[1024]; int pot = 0; int count = 0; MyBufferedInputStream(InputStream is) { this.is = is; } public int myRead()throws IOException { if(count==0) { count = is.read(by); if(count<0){ System.out.println("conut"); return -1; } pot = 0; byte b = by[pot]; pot++; count--; return b&0xff; } else if(count>0) { byte b = by[pot]; pot++; count--; return b&0xff; } return -1; }}源代码是为了包装InputStream 我的理解是,如果字符b(这里是如果)他代表8个二进制位1,他就是-127那么与运算之后11111111&0000000011111111,与了之后是不是也变成0000000011111111了,那是不是值为255呢? 我错了11111111 java中对应的是-1-127 对应的是 10000001 最后是255因为byte参与运算后结果上转为int了,且0xff一个字节以外的高位为0 ^ 我理解错楼主要问的意思了;b&0xff 的返回值是int型的…… ,但其值的大小不变 java 做小票打印跟一般的打印相同吗? TWaver问题! java.awt.Component 的handleEvent(Event evt) 如何固定jface中的Table组件的checkbox? ThreadPoolExecutor 有关java5线程池的问题 怎样实现双击label后转换为textfield 请看这段代码 有谁用过SourceForce.net的HtmlParser API,有事相求。 如何判断JCheckBox的状态?急,,,急 我的这个JSP为什么在tomcat下找不到自定义的类 这种工作环境要不要去 急? java 基础问题????
class MyBufferedInputStream
{
InputStream is;
byte[] by = new byte[1024];
int pot = 0;
int count = 0;
MyBufferedInputStream(InputStream is)
{
this.is = is;
}
public int myRead()throws IOException
{
if(count==0)
{
count = is.read(by);
if(count<0){
System.out.println("conut");
return -1;
}
pot = 0;
byte b = by[pot];
pot++;
count--;
return b&0xff;
}
else if(count>0)
{
byte b = by[pot];
pot++;
count--;
return b&0xff;
}
return -1;
}
}
源代码是为了包装InputStream
11111111&0000000011111111,与了之后是不是也变成0000000011111111了,那是不是值为255呢?
11111111 java中对应的是-1
-127 对应的是 10000001
最后是255
因为byte参与运算后结果上转为int了,且0xff一个字节以外的高位为0