标志值-33的意义是什么? [英] What is the significance of a flag value of -33?

查看:84
本文介绍了标志值-33的意义是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

'Example of usage:

	If Me.EditingComboBox IsNot Nothing AndAlso (Me._flags And 32) <> 0 Then
		RemoveHandler Me.EditingComboBox.DropDown, _ 
                    New EventHandler(AddressOf Me.ComboBox_DropDown)
		Me._flags = CByte((CInt(Me._flags) And -33))
	End If

推荐答案

编辑:

十进制 -33 是二进制 11111111111111111111111111011111 ,以便代码清除由零表示的位。从代码中可以看出这一点意味着什么;这就是为什么我们不想在这些开明的时代硬编码魔术数字。

Decimal -33 is binary 11111111111111111111111111011111 so that code will clear the bit represented by the zero. What that bit means is not evident from the code; this is a why we prefer not to hard-code magic numbers in these enlightened times.


reeselmiller2写道:
reeselmiller2 wrote:



感谢您的参考。这是我困惑的一部分。该标志是一个ulong,可以有一个像18446744073709551583这样的值转换为十进制-33,在C#中工作正常。但是在VB中如果你尝试我作为整数= CInt(18446744073709551583UL)你会得到一个溢出错误。所以你必须使用像Decimal.GetBits(18446744073709551583D)(0)这样的东西,它给你96位整数的低32位。我同意代码是可怕的,只是试图理解为什么它是这样写的。


Thanks for the reference. This is part of my confusion. The flag is a ulong and can have a value like 18446744073709551583 which converts to a decimal -33, works fine in C#. But in VB if you try "i As Integer = CInt(18446744073709551583UL) you get an overflow error. So you have to use something like Decimal.GetBits(18446744073709551583D)(0) which gives you the low 32bits of the 96 bit Integer. I agree the code is horrible, just trying to understand why it is written this way.

首先,我相信你无法理解为什么一些可怕的代码是可怕的;好吧,有人采取了草率的方式,那么什么?: - )



也许我可以解释这个令人困惑的部分。考虑一下这些事实:标志是ulong,但仍然,.NET中的按位运算是在枚举和整数类型上完成的转换为二进制意义上的最宽类型;特别是,签名语义完全被忽略。我建议你阅读 < u>两个补充 不仅适用于普通教育,更适合理解其主要思想:它允许CPU对歌剧的类型保持不可知算术运算的nds(不仅仅是逻辑运算,而是偶数+, - ,*,/):CPU指令以相同的方式对位进行操作,无论是否有一个或两个操作数都被签名。你明白了吗?有符号vs无符号是以语义方式解释算术结果的问题(在这种情况下,从该概念的数学意义上的整数值的角度来看),而实际比特保持不变。当然,按位算法也是如此。例如,对于16位整数,ushort 0xFFFF与short -1是同一个对象,因此是一个;就位而言,CPU无法看到任何差异。



现在,溢出错误来自类型案例从更广泛的类型到更窄类型的尝试,当一个值超出更宽类型的范围时。这种演员是语义而不是按位。也就是说,考虑了数字的符号性质及其数学语义。 (在C ++中,Bitwise强制转换称为reinterpret_cast。在.NET中,它通过类 System.BitConverter 完成: http://msdn.microsoft.com/en-us/library/system.bitconverter%28v=vs。 110%29.aspx [ ^ ]。)



至于方法, System.Decimal.GetBits ,它只是做它做的事情。实际上,它不返回32位值,而是返回4个32位整数的数组,如下所述: http://msdn.microsoft.com/en-us/library/system.decimal.getbits%28v=vs.110%29.aspx [<一个href =http://msdn.microsoft.com/en-us/library/system.decimal.getbits%28v=vs.110%29.aspxtarget =_ blanktitle =New Window> ^ ]。



(我不知道你为什么要关注这种方法。:-))



-SA

First of all, I believe you cannot understand why some horrible code is horrible; well, someone acted in a sloppy way, so what? :-)

Maybe I can explain the confusing part. Consider these facts: the flag is ulong, but still, bitwise operations in .NET are done on enumeration and integer types converted to a widest type in binary sense of it; in particular, the signed semantic is completely ignored. I advised you to read about two's complement not just for general education, but more for understanding of its main idea: it allows the CPU to remain agnostic about the type of the operands of the arithmetic operations (not just logical, but even +, -, *, /): the CPU instruction operates on bits in the same exact way, no matter if one or both operands are signed or not. Are you getting the idea? The signed vs unsigned is the matter of interpretation of the arithmetic result in semantic way (in this case, from the standpoint of the integer value in mathematical sense of this notion) while the actual bits remain the same. Of course, same goes for the bitwise arithmetic. For example, for 16-bit integers ushort 0xFFFF is the same object as short -1, and so one; in terms of bits, and CPU cannot "see" any difference.

Now, overflow error comes from the attempt of type case from a wider type to a narrower type, when a value is beyond the range of the wider type. This kind of cast is semantic and not bitwise. That is, the signed nature of the number and its mathematical semantics is taken into account. ("Bitwise cast", in C++, is called reinterpret_cast. In .NET, it is done via the class System.BitConverter: http://msdn.microsoft.com/en-us/library/system.bitconverter%28v=vs.110%29.aspx[^].)

As to the method, System.Decimal.GetBits, it just does what it does. Actually, it returns not 32-bit value, but an array of 4 32-bit integers, as described here: http://msdn.microsoft.com/en-us/library/system.decimal.getbits%28v=vs.110%29.aspx[^].

(I don't know why would you concern about this method. :-))

—SA


这篇关于标志值-33的意义是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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