获取字节中特定位的值 [英] Get values of specific bits in a byte

查看:116
本文介绍了获取字节中特定位的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,我有这个二进制数: 00100101 我想用后2位和其他0来创建一个新数字,如下所示: 00000001

我正在使用MIPS,所以我假设我必须使用逻辑或,或,和等的某种组合,但到目前为止没有成功.

解决方案

是旧版本,但它是搜索结果中排名最高的搜索引擎.我认为详细解释原因不会有伤害.

下面标题后的答案又快又脏

要获取特定位,一种方法是通过位掩码".位掩码表示您只会让某些位通过.就像当您遮罩要绘画的东西时一样.

逻辑与是我们想要的指令.逻辑运算比较各个位.它是任何数字化产品(如计算机)的中央操作.当您一路下滑时,所有芯片所要做的就是以新颖和创新的方式比较单个位.有2个运算和2个修饰符给出AND,OR,NOR,XOR,NAND,NOT,XNOR.

最后,我对逻辑比较器进行了总结.如果需要,请对其进行审查. (in retrospect, I think this is the most valuable part for a newbie.)

但是,如果我们将一点设置为我们想要的东西,那么另一方面,也可以得到我们想要的东西.说我想要位2、3、4和7.7- Y N N Y Y Y N N N-0正确.好吧,然后我创建一个带有1的蒙版. 1 0 0 1 1 1 0 0 0并进行逻辑与.

因此,这只是简单的数学运算.计算机说二进制.我只给它0 0 0 1 1 1 0 0对应的数字.

以二进制计数.最右边= 1,然后我们在每个位置加倍.我们也从0开始计数.因此右边的第0个位= 1,第一个位是2,第3个位= 4,然后是8、16、32、64、128.您可以获取0到255之间的任何值.(或127到-128),但是负数更为复杂.可能是相关的.

请务必注意,计算机语言中有两种整数类型-带符号无符号.最后一位-第7位(即您或我的第8位,价值128个无符号整数,但它等于有符号整数中的符号.令人惊讶的是,它们都具有相同的数字位数(或者也许一想到就不足为奇了

例如,让给定的数字[0 0 0 1 1 1 0 0]并使第一位为1 {1 0 0 1 1 1 0 0}.如果这是一个无符号整数,则其值156(128 + 16+ 8+ 4),但是如果第7位为符号位-计算起来有点复杂,您必须使用("Two's Complement" )得出1001110的值,虽然它有一个有趣的历史值得讨论,但这里不是.有符号位是通过翻转每个位来计算的-因为在负数中-0现在计为1,而1为0. {1 0 0 1 1 1 0 0}并将其设置为格式-{0 1 1 0 0 0 1 1},我们只需要记住它的负数,将其值翻转并加1:-1 + (64) + (32) + (2) + (1) = 99(-),然后我们必须记住减去-99 - 1 = -100.

我们减去一个是因为0000 0000为0.如果我们调用1111 1111 = 0,那么我们将有2个值等于0! (顺便说一句,这称为一个人的补语).因此,第-1行中的下一个数字.我相信最好有两个关于恭维的讨论,这不在本文的讨论范围之内.

所以你说, 值AND 28 = newValue 那么你可以做到这一点.

或执行FOR循环

for x in [0,1,2,4,8,16,32,64,-128]
    if (x AND value!= 0){
         y = log2(abs(x))
         print("the bit is a one in position" + y)
    } else {
        print("the bit is a 0 in position" + y)

    }

或者使用power方面进行遍历.

for(i = 1; i < 18, i++){
    byte y = 2 ** n
    if ((value & ubyte) == 0) { //is 0}

或这个基本相同的java示例:

class BitDemo {
    public static void main(String[] args) {
        int bitmask = 0x000F;
        int val = 0x2222;
        // prints "2"
        System.out.println(val & bitmask);
    }
}

快速解答在这里

由于这是一个有4年历史的问题,因此我旨在针对所有编程语言更普遍地回答该问题.在组装中,您需要发挥更大的创造力.如您所知,自您提出要求以来,每个操作都必须是离散的.真的迫使你思考.

li $t1, 0x020    # get the sixth bit, HEX 20 = DEC 32  in binary = 0b0010 0000
and $t2, $a0, $t1  #t1 gets the bit
slt $t3, $zero, $t2  # compares zero < t2 and if true stores
                       #1 into t3, so stores the 6th bit in its own register. 

使用slt,这是一条op指令,所有其他比较器都是伪指令,效率较低.更优雅.

您还可以将前两个说明与 psuedoinstruction 结合使用 **andi** $t2, $a0, 0x02但是编译器只会在后台将其转换为以上代码.我想保存一个寄存器.

另一种选择,如果您知道要查找的位,请移至其位0.

srl $v0, $t2, 5 # since we went after the 6th bit

我不喜欢使用andi(和即时方法,但是我不记得为什么.过去我有一些奇怪的行为,但是是解决上述问题的最短方法:

 #your value = $a0
 andi $v0,  $a0,  0x01     #already in first bit, no more work
 andi $v1,  $a0,  0x02    
 slr  $v1,  $v1, 1         #since we have 1 bit in 2nd bit, just shift one

在您的情况下,您需要最后两位,所以我们做1 + 2(位值)=3.这就是上面的答案为0x03的地方. 0x告诉汇编程序其十六进制bc汇编程序仅真正喜欢十六进制.如果要达到良好的效果(MARS),则需要十进制3.

---逻辑比较器复习/介绍- 有人告诉我我是个聪明人.但是当我开始的时候,所有这些对我来说真的很混乱,很难记住.直到我看了一会儿才意识到.
实际上非常简单.现在,我看着它,然后想一想,为什么这么难.比一年级的1 +1更容易.因此,如果您正在阅读此想法,那么这个人为什么要解释,这是因为您不记得第一次看到它了.

这样想,要容易得多,只需记住2个选择,记住2个操作,它们非常容易记住- AND OR (或两者都不选)进行逻辑比较.他们不是对立的,他们是不同的,并且按照他们说的做.然后有2个修饰符.而已.以AND和OR为名,了解形容词非常容易.

  • 不是 我们谈论的第一个是最简单的.它是一个修饰符,而不是一个运算符,它只需要一个值,因此它不是像 OR AND 这样的逻辑比较器.相反.如果为1,则表示0.如果为0,则为1.相反,什么也没有.在电路中,它将是电线.没有什么变化.如果仅取1值,它是如何工作的,我认为它是紧随其后的.您执行 OR AND 操作,然后将其翻转.所以 AND OR :

AND OR 很简单

  • AND -如果两个都是1(1 AND 1),则该值为1.这里没有别的. 1 AND 0为FALSE. 0和0为FALSE.仅当您出现去上班时,您才能获得报酬. FALSE和FALSE在该语句中不起作用.

  • OR 说,如果其中一个为1,则值为1.就这样.把书拿回来给我 OR 付我20美元,我不会打败你的.您可以做一个或另一个.如果两者都做怎么办?这很重要.好吧,您不会被殴打,所以1 OR 1的值是 TRUE .如果您不带书或带20美元,则不被殴打" = FALSE.

  • * X ***** [...] 现在,OR的最后一部分是重要的部分. 1 OR 1 = 1位,即您可以带来20美元或返还书的情况.因此,让我们进行逻辑比较,看看什么是好主意.两者都做是愚蠢的.这就是 X 的来源- X 用于e X 包含在内.严重地.它消除了 OR 中的一种特殊情况,其中,如果A或B为1,则 OR 为true.X说,不要使用1/1 = true选项,我想要独占1值true.这是很多人感到困惑的地方,或者至少是 是真的, XOR 说的完全是 1位为真.所以现在1 XOR 1 (=)0.

就是这些,您就拥有了所有的选择.

  • -它不是真正的比较器,只是将1个输入取反,但通常会混入
  • AND -两个比较值均为true,没有别的.
  • NAND -倒置了 AND ,因此,如果两个都为true,则为false,其他所有条件为true-(0,1)( 0,0)(1,).
  • OR -至少一个为true(1,0)(1,1)(0,1)= True (0,0)为假
  • NOR -反转OR,因此从上面开始,(1,0)(1,1)(0,1)= False,而(0,0)是真的.
  • XOR -消除OR中的特殊情况(1,1)= true. X仅在 OR 上起作用,现在说正好为1.
    • XNOR -这将使您的头部受伤.但是,只需使用真值表逐步进行操作即可.我们从 OR 开始,并删除(1,1)= true,因此当这些位匹配= false时,以及当它们不同时,它们的真(1,0)(0,1).然后将其反转.所以现在,当位不同(1,0)(0,1)时为假.但是当他们匹配时是真的. (1,1)和(0,0),所以这是一个 AND 门,以及一个反向的 AND ( NAND )门,并通过 OR/XOR 门连接. (X无关紧要,因为除非我们在谈论量子问题,否则您不能使两个位同时为1,1和0,0.

For example, I have this binary number: 00100101 And I want to make a new number with only the last 2 bits and the others 0, like so: 00000001

I am working with MIPS so I assume I must use some combination of logical or, xor, and, ect but unsuccessful so far.

解决方案

Old, but it is the top of the search results as the top hit. I don't think it will hurt to explain why in some detail.

Quick and dirty answer after the heading below

To get specific bits, one way to do it is by a "bitmask". A bitmask says that you are only going to let certain bits through. Just like when you mask something to paint.

logical AND is the instruction we want. Logical operations compare individual bits. Its the central operation of anything digital- anything that operates like a computer. When you get all the way down, all any chip is doing is comparing single bits in new and creative ways. There are 2 operations and 2 modifiers giving AND, OR, NOR, XOR, NAND, NOT, XNOR.

I threw a summary on Logical Comparators at the end. Review it if needed. (in retrospect, I think this is the most valuable part for a newbie.)

But if we set one bit to be what we want, then on the other side well get what we want. Say I want bits 2, 3, 4, and 7. 7- Y N N Y Y Y N N - 0 right. Well, then I create a mask with 1's there. 1 0 0 1 1 1 0 0 and do logical AND.

So then it's just simple math. The computer speaks binary. I just give it the number that 0 0 0 1 1 1 0 0 corresponds to.

Counting in binary. Far right = 1, and then we double in each position. We also start counting at 0. So the 0th bit on the right = 1, the first bit is 2, the 3rd =4, then 8, 16, 32, 64, 128. Play with this if you want, but using this number you can get any value from 0 to 255. (or 127 to -128) but the negatives are more complicated. Might be relevant though.

It's important to note that there are two types of integers in a computer language- Signed, and Unsigned. That last bit- the 7th bit (which is the 8th bit to your or I, is worth 128 an unsigned int, but it's just equal to the sign in the signed int. Surprisingly, they both have the same number of digits (or maybe not surprisingly once you think about it)

As an example, lets take the given number [0 0 0 1 1 1 0 0] and make the first bit a 1 {1 0 0 1 1 1 0 0}, If this is an unsigned integer, its worth 156 (128 + 16+ 8+ 4), but if the 7th bit is a sign bit-- Calculating it is slightly complicated. You must use ("Two's complement") to get the value of 1001110. And while it has a fascinating history that is worth discussing, just not here. The signed bit is calculated by flipping every bit- because in negative numbers- 0 now counts as 1's and 1's are 0's. So lets take {1 0 0 1 1 1 0 0} and make it a format were used to- {0 1 1 0 0 0 1 1}, we just have to remember its negative. The value of it, flipping and add 1: -1 + (64) + (32) + (2) + (1) = 99(-) and then we must remember to subtract -99 - 1 = -100.

We subtract one because 0000 0000 is 0. And if we called 1111 1111 = 0 then we would have 2 values for 0! (incidentally, this is called One's Complement). So, the next number in line- -1!. I am sure there are better two's compliment discussions and its out the scope of this.

So you say, Value AND 28 = newValue then you can AND that.

or do a FOR loop

for x in [0,1,2,4,8,16,32,64,-128]
    if (x AND value!= 0){
         y = log2(abs(x))
         print("the bit is a one in position" + y)
    } else {
        print("the bit is a 0 in position" + y)

    }

Or do a for loop using the power aspect to iterate through.

for(i = 1; i < 18, i++){
    byte y = 2 ** n
    if ((value & ubyte) == 0) { //is 0}

or this java example which is basically the same:

class BitDemo {
    public static void main(String[] args) {
        int bitmask = 0x000F;
        int val = 0x2222;
        // prints "2"
        System.out.println(val & bitmask);
    }
}

Quick Answer is here

I've aimed to answer the question more generally for all programming languages since this is a 4 yr old question. In assembly, you need to get more creative. As you know, since youre asking, every operation has to be discrete. Really forces you to think.

li $t1, 0x020    # get the sixth bit, HEX 20 = DEC 32  in binary = 0b0010 0000
and $t2, $a0, $t1  #t1 gets the bit
slt $t3, $zero, $t2  # compares zero < t2 and if true stores
                       #1 into t3, so stores the 6th bit in its own register. 

use slt, it's a single op instruction, all the other comparators are pseudo instructions, which is less efficient. It's more elegant.

You can also combine the first two instructions with the psuedoinstruction **andi** $t2, $a0, 0x02 But the compiler will just convert it to the above in the background. I guess save a register.

Another option, if you know the bit you are looking for is to shift until its bit 0.

srl $v0, $t2, 5 # since we went after the 6th bit

I don't like using andi (and immediete, but I can't remember why. I got some weird behavior in the past, but the shortest way to solve the above:

 #your value = $a0
 andi $v0,  $a0,  0x01     #already in first bit, no more work
 andi $v1,  $a0,  0x02    
 slr  $v1,  $v1, 1         #since we have 1 bit in 2nd bit, just shift one

In your case, you want the last two bits, so we do 1 + 2 (the bit values)= 3. That's where the answer above gets 0x03. 0x tells the assembler its hex bc the assembler only really likes hex. Though it would take decimal 3 if its good (MARS).

---logical comparators review/intro--- I'm a smart dude I am told. But when I started out all these were really confusing to me and hard to remember. That was until I looked at it for a while and realize something.
It's actually really simple. Now I look at it and think, why was that ever hard. It's easier than 1 + 1 in 1st grade. So if you're reading this thinking why is this guy explaining, its because you don't remember the first time you saw it.

Think of it this way, and it's much easier there are only 2 choices, 2 operations to remember and they're super easy to remember- AND and OR (or neither) for logical comparisons. They're not opposites, they're different, and they do what they say. Then there are 2 modifiers. That's it. Understanding AND and OR are in the name, and knowing the adjectives is super easy.

  • NOT the first one we talk about is the easiest. It's a modifier, more than an operator- it only takes one value, so it's not a logical comparator like OR or AND. It just does the opposite. If it's 1, it says 0. If it's 0, then it's 1. Its opposite is nothing. In a circuit, it would be a wire. Nothing changes. How does it work if it only takes 1 value, I think of it as coming after. You do the OR or AND operation, then flip it. So AND and OR:

AND and OR are simple,

  • AND- if both things are 1 (1 AND 1) then the value is one. Nothing else is true here. 1 AND 0 is FALSE. 0 and 0 are FALSE. You only get paid if you show up to work AND do the work. The FALSE and FALSE wouldn't work in that statement.

  • OR says that if either is 1 then the value is one. That's all. Bring me the book back OR pay me $20 and I won't beat you up. You can do one or the other. What if you do both? That's the important bit. Well, you won't get beaten up so the value of 1 OR 1 is TRUE. IF you don't bring the book or bring $20 then 'not getting beat up'= FALSE.

  • *X*****[...] Now that last part from the OR was the important part. The 1 OR 1 = 1 bit, the scenario where you can bring $20 OR return the book. So let's do a logical comparison to see what is a good idea. Doing both is kinda dumb. That's where X comes in- the X is for eXclusive. Seriously. It gets rid of a special case in OR where OR is true if A or B is 1. X says, don't use the 1 / 1 = true option, I want exclusively 1 value true. That is where a lot of people get confused, OR is at least one bit is true, XOR says exactly 1 bit is true. So now 1 XOR 1 is(=) 0.

That's it from those you get all the options.

  • NOT - it's not really a comparator, just inverts 1 input, but usually lumped in
  • AND - both compared values are true, nothing else.
  • NAND - inverted AND, so if both are true, then is false, everything else is true- (0,1)(0,0)(1,).
  • OR - At least one is true (1,0)(1,1)(0,1) = True, only (0,0) is false
  • NOR- Inverts OR, so from above, (1,0)(1,1)(0,1) = False, and (0,0) is true.
  • XOR - Gets rid of the special (1,1) = true case in OR. The X only works on OR, Says now exactly 1 is true.
    • XNOR - This one will make your head hurt. But just do it stepwise with a truth table. We start with OR, and remove (1,1) = true, so when the bits match = false, and when they are different its true (1,0)(0,1). then invert it. So now, it's false when the bits differ (1,0)(0,1). But it's true when they match. (1,1) and (0,0), So this is an AND gate, and an inverted AND (NAND) gate joined by an OR/XOR gate. (The X is irrelevant, as you cant have both bits simultaneously be 1,1 and 0,0 unless we're talking quantum stuff.

这篇关于获取字节中特定位的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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