按位或(在数组) [英] bitwise OR (on array)

查看:203
本文介绍了按位或(在数组)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要执行位或在Java字节的两个数组。我该怎么办呢?

 字节=新的字节[256];
字节B =新的字节[256];字节℃; / *它应包含的信息即按位或a和b * /


解决方案

这就是简单,只要使用|操作员和一个循环

 公共静态的byte [] byteOr(字节[]一,字节[] B){
    INT LEN = Math.min(则为a.length,b.length个);
    字节[]结果=新的字节[LEN]
    的for(int i = 0; I< LEN ++ I)
        结果[I] =(字节)(A [I] | B〔I])
    返回结果;
}

I need to perform bitwise OR of two arrays of byte in Java. How can I do so?

byte a= new byte[256];
byte b= new byte[256];

byte c; /*it should contain information i.e bitwise OR of a and b */

解决方案

Thats as simple as using the | operator and a loop:

public static byte[] byteOr(byte[] a, byte[] b) {
    int len = Math.min(a.length, b.length);
    byte[] result = new byte[len];
    for (int i=0; i<len; ++i)
        result[i] = (byte) (a[i] | b[i])
    return result;
}

这篇关于按位或(在数组)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆