在这篇文章中,我们将为您详细介绍将union字段中的位解释为C/C++中的不同数据类型的内容,并且讨论关于union字段可以不同吗的相关问题。此外,我们还会涉及一些关于2.14定义不同数据类型的变量、
在这篇文章中,我们将为您详细介绍将union字段中的位解释为C/C++中的不同数据类型的内容,并且讨论关于union字段可以不同吗的相关问题。此外,我们还会涉及一些关于2.14 定义不同数据类型的变量、c – 将unsigned解释为已签名、C#中的数据类型是什么?C#中的四种数据类型解释、java 不同数据类型的相互转化的知识,以帮助您更全面地了解这个主题。
本文目录一览:- 将union字段中的位解释为C/C++中的不同数据类型(union字段可以不同吗)
- 2.14 定义不同数据类型的变量
- c – 将unsigned解释为已签名
- C#中的数据类型是什么?C#中的四种数据类型解释
- java 不同数据类型的相互转化
将union字段中的位解释为C/C++中的不同数据类型(union字段可以不同吗)
typedef union { uint64_t x; uint32_t y[2]; }test; test testdata; testdata.x = 0xa; printf("uint64_t: %016lx\nuint32_t: %08x %08x\n",testdata.x,testdata.y[0],testdata.y[1]); printf("Addresses:\nuint64_t: %016lx\nuint32_t: %p %p\n",&testdata.x,&testdata.y[0],&testdata.y[1]);
输出是
uint64_t: 000000000000000a uint32_t: 0000000a 00000000 Addresses: uint64_t: 00007ffe09d594e0 uint32_t: 0x7ffe09d594e0 0x7ffe09d594e4
y指向的起始地址与x的起始地址相同.由于两个字段使用相同的位置,x的值不应该是00000000 0000000a吗?
为什么不发生这种情况?内部转换如何在具有不同数据类型的不同字段的联盟中发生?
需要做什么来使用联合以与uint64_t中相同的顺序检索精确的原始位作为uint32_t?
编辑:
正如评论中所提到的,C给出了未定义的行为.
它在C中如何工作?我们真的可以这样做吗?
解决方法
您正在uint64_t值和2个uint32_t值的数组之间进行类型惩罚.根据结果,您的系统是小端,并且很乐意通过简单地重新解释字节表示来接受类型惩罚.并且0x0a的字节表示为小端uint64_t是:
Byte number 0 1 2 3 4 5 6 7 Value 0x0a 0x00 0x00 0x00 0x00 0x00 0x00 0x00
little endian中的最低有效字节具有最低地址.现在很明显为什么uint32_t [2]表示为{0x0a,0x00}.
但是你所做的只是在C语言中是合法的.
C语言:
C11表示为6.5.2.3结构和工会成员:
3 A postfix expression followed by the . operator and an identifier designates a member of
a structure or union object. The value is that of the named member,95) and is an lvalue if
the first expression is an lvalue.
95)说明明确说:
If the member used to read the contents of a union object is not the same as the member last used to
store a value in the object,the appropriate part of the object representation of the value is reinterpreted
as an object representation in the new type as described in 6.2.6 (a process sometimes called ‘‘type
punning’’). This might be a trap representation.
因此,即使注释不是规范性的,它们的目的是明确标准应该被解释的方式=>代码是有效的,并且在定义uint64_t和uint32_t类型的小端系统上定义了行为.
C语言:
C部分更严格. C17的草案n4659在[basic.lval]中说明:
8 If a program attempts to access the stored value of an object through a glvalue of other than one of the
following types the behavior is undefined:56
(8.1) — the dynamic type of the object,
(8.2) — a cv-qualified version of the dynamic type of the object,
(8.3) — a type similar (as defined in 7.5) to the dynamic type of the object,
(8.4) — a type that is the signed or unsigned type corresponding to the dynamic type of the object,
(8.5) — a type that is the signed or unsigned type corresponding to a cv-qualified version of the dynamic type
of the object,
(8.6) — an aggregate or union type that includes one of the aforementioned types among its elements or nonstatic
data members (including,recursively,an element or non-static data member of a subaggregate or
contained union),
(8.7) — a type that is a (possibly cv-qualified) base class type of the dynamic type of the object,
(8.8) — a char,unsigned char,or std::byte type.
注释56明确地说:
The intent of this list is to specify those circumstances in which an object may or may not be aliased.
因为在C标准中从未引用过双关语,并且因为结构/联合部分不包含C的重新解释的等价物,这意味着在C中读取不是最后写入的成员的值会调用undefined行为.
当然,常见的编译器实现编译C和C,并且大多数它们甚至在C源中也接受C语言,因为gcc C编译器很乐意接受C源文件中的VLA.毕竟,未定义的行为包括预期的结果……但是你不应该依赖它来获取可移植的代码.
2.14 定义不同数据类型的变量
/*
数据类型:Java是一种强类型的语言,针对每一种数据都定义了明确的数据类型。
数据类型分类:
A:基本数据类型
B:引用数据类型(类,接口,数组)
基本数据类型:4类8种
A:整数 占用字节数
byte 1
short 2
int 4
long 8
B:浮点数
float 4
double 8
C:字符
char 2
D:布尔
boolean 1
注意:
整数默认是int类型
浮点数默认是double类型。
长整型后缀用L或者l标记。建议使用L。
单精度浮点数用F或者f标记。建议使用F。
*/
class DataTypeDemo {
public static void main(String[] args) {
// 定义变量的格式:
// 数据类型 变量名 = 初始化值;
// 定义一个字节变量
byte b = 10;
System.out.println(10);
System.out.println(b);
// 定义一个短整型变量
short s = 100;
System.out.println(s);
// 定义一个整型变量
int i = 1000;
System.out.println(i);
// 超过了int的范围
// int j = 1000000000000;
long j = 1000000000000L;
// long j = 100L;
System.out.println(j);
// 定义浮点数据变量
float f = 12.345F;
System.out.println(f);
double d = 12.345;
System.out.println(d);
// 定义字符变量
char ch = ''a'';
System.out.println(ch);
// 定义布尔变量
boolean flag = true;
System.out.println(flag);
}
}
c – 将unsigned解释为已签名
uint8_t foo = 0xCE; // 0b11001110
解释为无符号,这将是206.但实际上它是签名的,因此类似于-50.如何继续使用此值作为签名?
int8_t bar = foo; // doesn't work
两者都没有(导致所有输入值的0x10或0x00)
int8_t bar = static_cast<int8_t>(foo); int8_t bar = reinterpret_cast<int8_t&>(foo);
我只是希望这些位保持不变,即. (bar == 0xCE)
反之亦然我感兴趣的是如何将代表负数的位模式转换为无符号变量而不会弄乱位模式.我正在使用GCC.
解决方法
int x = (signed char)(foo);
在C中,您还可以说:
int x = static_cast<signed char>(foo);
请注意,促销始终会在重新解释位模式之前尝试保留该值.因此,您首先必须转换为与无符号类型相同大小的签名类型,以强制进行签名重新解释.
(当我尝试将字符作为十六进制数字对打印时,我通常面临相反的问题.)
C#中的数据类型是什么?C#中的四种数据类型解释
C#语言带有一组基本数据类型。这些数据类型用于构建应用程序中使用的值。我们来探索C#中可用的基本数据类型。对于每个示例,我们将仅修改Program.cs文件中的main函数。【推荐阅读:C#视频教程】
1.整数
Integer数据类型用于处理数字。在这种情况下,数字是整数,如10,20或30.在C#中,数据类型由Int32关键字表示。下面是如何使用此数据类型的示例。在我们的示例中,我们将定义一个名为num的Int32变量。然后,我们将为变量分配一个Integer值,然后相应地显示它。
static void Main(string [] args) { Int32 num = 30; Console.Write(NUM); Console.ReadKey(); } } }
代码说明
Int32数据类型来声明名为num的Integer变量,然后变量赋值30。最后,console.write函数用于向控制台显示数字。
如果正确输入上述代码并且程序执行成功,将显示以下输出。输出:
从输出中,您可以清楚地看到控制台中显示名为num的Integer变量
2.双精度浮点型
如果在这种情况下,数字是整数,如10.11,20.22或30.33。在C#中,数据类型由关键字“ Double ”表示。下面是此数据类型的示例,我们将定义一个名为num的双变量。然后,我们将为变量分配Double值,然后相应地显示它。
{ static void Main(string [] args) { double num = 30.33; Console.Write(NUM); Console.ReadKey(); } } }
指定double数据类型以声明名为num的double类型变量。然后为变量赋值30.33
输出:
3.布尔值
布尔数据类型用于处理true和false的布尔值。在C#中,数据类型由Boolean关键字表示。我们将定义一个名为“status”的布尔变量。然后,我们将为变量分配一个布尔值,然后相应地显示它。
{ static void Main(string[] args) { Boolean status=true; Console.Write(status); Console.ReadKey(); } } }
指定布尔数据类型以声明名为“status”的布尔变量。然后为变量赋值true/false,输出:
4字符串
String数据类型用于处理String值。在C#中,数据类型由关键字“String”表示。
{ static void Main(string [] args) { String message =“hello”; Console.Write(message); Console.ReadKey(); } } }
指定String数据类型以声明名为message的字符串变量。然后为变量赋值“Hello”。console.write函数用于向控制台显示字符串值,输出:
以上就是对C#中的数据类型是什么?C#中的四种数据类型解释的全部介绍,如果你想了解更多请关注php中文网。
以上就是C#中的数据类型是什么?C#中的四种数据类型解释的详细内容,更多请关注php中文网其它相关文章!
java 不同数据类型的相互转化
在工作中经常会遇到需要将数据类型转化的情况,今天抽出时间总结一下。
date——string
Date date = new Date();
DateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
String StringDate = String.valueOf(dateformat.format(date));
string——date
String stringDate= "2018-02-01 11:11:11";
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
try{
Date date = dateFormat.parse(stringDate);
System.out.println(date);
}
catch(ParseException e)
{
e.printStackTrace();
}
int——double
int intNum = 12345;
String stringNum = String.valueOf(intNum);
double doubleNum1 = Double.parseDouble(stringNum);//Double.parseDouble返回的是基本数据类型double
DecimalFormat df = new DecimalFormat("#.00");
String dfString = df.format(doubleNum1);
System.out.println(dfString);//在jdk1.5之后的可以自由相加。
double——int
double doubleNum = 1.545;
DecimalFormat df = new DecimalFormat("0");
int intNum = Integer.parseInt(df.format(doubleNum));//这是四舍五入,非四舍五入直接强转。
今天关于将union字段中的位解释为C/C++中的不同数据类型和union字段可以不同吗的介绍到此结束,谢谢您的阅读,有关2.14 定义不同数据类型的变量、c – 将unsigned解释为已签名、C#中的数据类型是什么?C#中的四种数据类型解释、java 不同数据类型的相互转化等更多相关知识的信息可以在本站进行查询。
本文标签: