C语言——移位操作符

【零天赋C语言】——详解操作符 一、算术操作符 除了 % 操作符之外,其他的几个操作符可以作用于整数和浮点数。 对于 / 操作符如果两个操作数都为整数,执行整数除法。而只要有浮点数执行的就是浮点数除法。 % 操作符的两个操作数必须为整数。返回的是整除之后的余数。 二、移位操作符 整数的二进制表示有三种方式: 原码 反码 补码 正数的原码、反码、补码相同; 负数的原码、反码、补码是需要计算的; 一、左移操作符 : << 移位规则:左边抛弃,右边补零 注:n向左移动一位之后,n的值并未发生.. 阅读详情

目录

左移操作符

右移操作符 

算术右移

 逻辑右移

总结


内存中存储的是二进制的补码。

所以移位操作符是对二进制补码进行移位。

先以左移操作符为例:<<


左移操作符

正数的情况:

#include<stdio.h>
int main()
{int a=10;
//00000000000000000000000000001010——补码  //正数原码反码补码相同
int b=a<<1;  //左移1
printf("%d\n",b);
printf("%d\n",a);
return 0;
}

过程:结果:

注意:在此过程中,a并没有变化。

负数的情况:

#include<stdio.h>
int main()
{int a=-10;
//10000000000000000000000000001010——原码
//11111111111111111111111111110101——反码   //取反
//11111111111111111111111111110110——补码   //加一
int b=a<<1;  //左移1
//11111111111111111111111111101100——补码
//11111111111111111111111111101011——反码   //减一
//10000000000000000000000000010100——原码   //取反
printf("%d\n",b);
printf("%d\n",a);
return 0;
}

过程:结果:

 注意:我们打印的是原码。

            a依旧没变。

发现:1.规律是:左边抛弃,右边补零。

           2.左移一位有乘2的效果。


右移操作符 

右移操作符分为算术右移和逻辑右移。

这取决于编译器,算术右移比较常见。

 

算术右移

#include<stdio.h>
int main()
{
	int a = -1;
	//10000000000000000000000000000001——原码
	//11111111111111111111111111111110——反码
	//11111111111111111111111111111111——补码
	
    int b = a>>1;
    //11111111111111111111111111111111——补码
    //11111111111111111111111111111110——反码
    //10000000000000000000000000000001——补码
	
    printf("%d\n", b);
	printf("%d\n", a);
	return 0;
}

 过程:结果:

 

 逻辑右移

#include<stdio.h>
int main()
{
	int a = -1;
	//10000000000000000000000000000001——原码
	//11111111111111111111111111111110——反码
	//11111111111111111111111111111111——补码
	
    int b = a>>1;
    //01111111111111111111111111111111——补码
    //01111111111111111111111111111110——反码
    //00000000000000000000000000000001——补码
	
    printf("%d\n", b);
	printf("%d\n", a);
	return 0;
}

过程:

 发现:1.算术右移:右边抛弃,左边补原来的符号位。

            2.逻辑右移:右边抛弃,左边补零。


总结

左移操作符:左边抛弃,右边补零。

算术右移:右边抛弃,左边补原来的符号位。

逻辑右移:右边抛弃,左边补零。

注意:不存在右移/左移负数的情况。

【位运算进阶之----右移(>>)】 😄嘻嘻,朋友们,大家好!昨天我们学习了左移,今天我们来谈谈右移>>。⭐️简单来说,,也就是去掉一个数的二进制表达的末位,右移一位就去掉一位,右移两位就去掉两位。下面就让我们来详细看看吧! 阅读详情

相关推荐

理解C语言中的左移(<<)与右移(>>)运算符

C语言中的左移(<<)和右移(>>)运算符用于二进制位操作。左移将数值向左移动指定位数,右侧补0,相当于乘以2的n次方;右移将数值向右移动,无符号数左侧补0,有符号数通常补符号位,相当于除以2的n次方取整。使用时需注意移位数的有效范围(0到类型位宽-1)和有符号数的实现差异。位运算比乘除法更高效,适合性能敏感场景。掌握这些操作有助于底层编程和性能优化。

学无止境 4786

C语言左移右移操作符详解

此文章由博主查阅多处博客、课程整理,主要面向初学者,同时又不乏深度,由浅及深,助力初学者更好地掌握C语言左移右移操作符。

工业废气 1万+

[小技巧] 如何复制知乎电子书中的文字和图片

文章目录一、背景二、复制文字1.最简单的方法(适合短小文字)2. 进阶方法(适合大段文字)三、复制图片四、注意事项 一、背景 有时候希望从书中摘录一部分文字作为笔记,但是知乎电子书阅读时候是禁止copy的,要么手打,要么OCR,给学习带来了一些不便。 审查元素后发现,知乎电子书是把文字分成一段一段的,试图F12直接copy不太方便。 本问将以谷歌Chrome浏览器为例,给出复制小技巧。 二、复制文字 1.最简单的方法(适合短小文字) 选中你需要的文字,左键单击不放,直接拖到地址栏。 然后可以从地址栏中

江南蜡笔小新 9246

逻辑右移与算术右移

逻辑右移与算术右移

一只青木呀 1万+

电压比较器---滞回比较器(施密特触发器)

当输入电压从小电压开始增加,当其大于VT+时,电压比较器输出低电平,此时阈值电压变成下门限电压(VT-);当输入电压从大电压开始减小,当其小于VT-时,电压比较器输出高电平,此时阈值电压变成上门限电压(VT+);只有当第一次超过上门限电压或者下门限电压值,电压比较器的输出才会翻转变化;Uo为比较输出电压,有两种电压状态VoH(高电平)和VoL(低电平);当Uo输出为高时,电压比较器的阈值电压是上门限电压(VT+);当Uo输出为低时,电压比较器的阈值电压是下门限电压(VT-);Vref为比较参考电压;

weixin_69076249的博客 1万+

C++(Qt)软件调试---静态分析工具clang-tidy(18)

现在很多人在开发中完全忽略了编译器、IDE的警告提示,这怎么可能写出稳定的程序。 clang-tidy是一个由LLVM项目提供的开源工具,是一个静态分析工具,用于进行静态代码分析和代码质量改进。

mahuifa的博客 5079

【c 语言 】移位操作符详解

本文将详细解析C语言中移位操作符的工作原理、基本语法、应用场景以及注意事项和常见问题,帮助读者更好地掌握这一强大的工具。

m0_74195174的博客 5162

Linux C/C++ or 嵌入式面试之《C/C++笔面试系列》(10) C++ 前置后置增减运算符重载形式与比较

C语言的++、–运算符在此不讨论。 文章目录1、前置后置增减运算符重载的形式2、前置和后置运算符的性能比较 1、前置后置增减运算符重载的形式 自增运算符“++”和自减运算符“–”分别包含两个版本。即运算符前置形式(如 ++x)和运算符后置形式(如 x++),这两者进行的操作是不一样的。因此,当我们在对这两个运算符进行重载时,就必须区分前置和后置形式。C++根据参数的个数来区分前置和后置形式。如果按照通常的方法来重载 ++ 运算符(即作为成员函数重载时不带参数,或者作为非成员函数重载时带有一个参数),那么重载

二进制君 5829

C语言-【移位操作符详解】

这篇文章主要介绍了C语言中移位操作符

yyzzzj0301的博客 5495

液态神经网络(LNN):打破AI“静态快照”的连续动力学革命---AI大模型系统从零开始0029

MIT研究团队受秀丽隐杆线虫神经系统的启发,开发出革命性的液态神经网络(LNN)。该技术通过连续微分方程建模神经元动态,实现输入响应速度的自适应调节,解决了传统AI模型静态化、高能耗和低解释性的痛点。LNN具有三大突破性优势:仅需少量神经元(如20个)即可完成复杂任务;能处理非均匀时序数据;在自动驾驶、医疗监测等领域展现出卓越的适应性和鲁棒性。这项将生物智能与数学建模完美融合的技术,有望成为下一代AI的核心架构,目前已在无人机导航等场景验证了其高效性,被预测为2026年最具潜力的AI突破方向。

添柴程序猿的专栏 370

PyTorch深度学习实战(25)——从零开始实现Mask R-CNN实例分割

Mask R-CNN 是一种在目标检测任务中引入了语义分割的强大框架,通过在 Faster R-CNN 基础上进行扩展,添加了额外的分支网络,不仅可以准确地检测对象的位置和类别,还可以生成每个实例的精确像素级别的语义分割掩码。其模块化的设计可以轻松地应用于不同的任务和数据集,并且可以通过添加更多的分支进行功能扩展,如实例关键点检测等。

盼小辉丶的博客 1万+

20.《C语言》——【移位操作符】

C语言移位操作符详解

一名普通高校生 1095

vue2 - elementUI表格table树形结构复选框勾选父子联动示例,el-table树状表格tree复选框checkbox多选框全选/反选取消children(当子级全选中,父级自动勾选)

vue2,elementui,el-table树状表格多选,树形结构结合多选框使用,实现单选父子联动全选反选,勾选联动父级勾选,表格多选check父子联动,table表格children如何嵌入复选框,el-table使用多选框与树形数据写一个有半选全选功能的表格,el-table树形表格实现复选框多选效果,表格树状结构多选,每一个row都可以选中并且联动全选按钮,table选父带子选子带父,树形数据与懒加载,Table表格树形结构多选框选中父级时会选中子级,elementUI表格里树形结构嵌套复选框全选全

✅ 精选推荐文章 1万+

C语言移位操作符的使用详解

我们可以得出结论:对任意一个值a对他左移()n位,对应值的变化位:a /2^N

Ultraman_leo77的博客 1163

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

温度PID控制PLC程序

温度PID稳定控制程序,适用于欧姆龙PLC,对其他PLC也具有很大参考价值

新手入门C语言之移位操作符和位操作符

C语言中移位操作符与位操作符的概念以及性质,其中涉及进制以及进制之间的相互转化,原码反码补码的书写,以及一些比较有特点的例题附带解析

qq_73509351的博客 2461

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

【C语言】位与移位操作符详解

对于C语言中位操作符与移位操作符的介绍

Renswc的博客 2937

NTscan扫描工具

ntscan是一个扫描器,可以扫描没有密码和弱密码的共享,先到配置文件中填写猜测的用户名和密码,扫描过程中右侧出现的是扫描日志。

C语言——操作符(上)算术、移位、位、赋值操作符

算数操作符、移位操作符、位操作符、赋值操作符

小小小智_x0__0x_的博客 3278

C语言中原码,反码,补码与移位操作符

我们现在学习一下C语言中移位操作符的使用,与原码、补码、反码的概念与使用,在原码补码反码中,正整数三个都相同,负数的话我们在下面详细讲解。原码反码补码的概念:他们是整数的二进制表示的三种方式。

qq_64057396的博客 1198

C语言的移位操作符

C语言的移位操作符  位移位运算符是将数据看成二进制数,对其进行向左或向右移动若干位的运算。位移位运算符分为左移和右移两种,均为双目运算符。第一运算对象是移位对象,第二个运算对象是所移的二进制位数。  位移位运算符的运算对象、运算规则与结果、结合性如表2-16所示。  移位时,移出的位数全部丢弃,移出的空位补入的数与左移还是右移花接木有关。如果是左移,则规定补入的数全部是0

深度Java 8万+

C语言算数操作符和移位操作符

C语言的算数操作符和移位操作符的简答介绍。C语言的算数操作符和移位操作符的简答介绍。

Farewell_me的博客 558

C语言位运算详解(移位操作符、位操作符)

前言:C语言的位运算是基于整数的运算,因为C语言语法规定了移位操作符、位操作符的操作数只能是整数。

喜羊羊的博客 6233

C语言--移位操作符和位操作符应用

C语言中有很多操作符,可能初学者对移位操作符和位操作符不是太了解,但是在嵌入式开发中对移位操作符和位操作符的应用非常多。所以本博客主要讲解移位操作符和位操作符的编程应用。移位操作符和位操作符的使用,可以让一些问题变得非常简便。但是有时候并不容易想到,只要我们勤于练习,定能把它使用的游刃有余。

分享学到的技术~~ 975

C语言中的移位操作符与原码、补码、反码

任意一种记法均可。

编程知识点梳理、项目经验总结、学习方法分享 2538

(c语言)移位操作符

/反码:11111111111111111111111111111000 (原码的符号位不变,其余位按位取反就是反码)//00000000000000000000000000001110——左移1位后(左移后补0) 14。//补码:11111111111111111111111111111001 (反码加1就是补码)//00000000000000000000000000000111——补码 7。//00000000000000000000000000000111——补码 7。

天涯路的专栏 1650

【C语言】关于移位操作符中的原码、反码和补码

整数的二进制表示有3种,分别为,而整数在内存中的是。其中正整数的原码,反码和补码都是相同的。而负整数的原码,反码和补码都是需要计算的。

wacanda的博客 1182
上一篇: C语言——二进制(原码、反码、补码)
下一篇: C语言——位操作符
outdated_socks
博客等级 码龄4年 185粉丝 17原创
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值